Launch python file in Roslaunch command

asked 2022-07-11 05:39:56 -0500

Aeonoq gravatar image

Hi!

I need some help running a python script from a roslaunch command. I have a RPlidar connected to my raspberry pi which is ROS configured. I have a special software part of RViz called Hector Slam installed. Also connected to the raspberry pi is an actuator running a simple program.

When I type roslaunch hector_slam_launch tutorial.launch , I want a python file to activate which starts the actuator.

Simple Actuator Code:

from gpiozero import AngularServo
from time import sleep

servo = AngularServo(18, min_pulse_width=0.0006, max_pulse_width=0.0023)

while (True):
    servo.angle = 90
    sleep(2)
    servo.angle = 0
    sleep(2)
    servo.angle = -90
    sleep(2)

Now I have called this file servotest.py. I have placed it in the directory hector_slam_launch as well and I have used the command in terminal that makes it an executable (chmod +x servotest.py). In the terminal though if I do ./servotest it comes up with this error in terminal:

from: can't read /var/mail/gpiozero
from: can't read /var/mail/time
./servotest.py: line 3: $'\r': command not found
./servotest.py: line 4: syntax error near unexpected token `('
./servotest.py: line 4: `servo = AngularServo(18, min_pulse_width=0.0006, max_pu'se_width=0.0023)

This python file works perfectly when launching Thonny (python IDE) and clicking run.

I have also added in the tutorial.launch file lines 8 and 9 (the two lines beginning with <node> and </node>):

<?xml version="1.0"?>

<launch>

  <arg name="geotiff_map_file_path" default="$(find hector_geotiff)/maps"/>

  <param name="/use_sim_time" value="false"/>
  <node pkg="hector_slam_launch" type="servotest.py" name="servotest">
  </node>
  <node pkg="rviz" type="rviz" name="rviz"
    args="-d $(find hector_slam_launch)/rviz_cfg/mapping_demo.rviz"/>

  <include file="$(find hector_mapping)/launch/mapping_default.launch"/>

  <include file="$(find hector_geotiff_launch)/launch/geotiff_mapper.launch">
    <arg name="trajectory_source_frame_name" value="scanmatcher_frame"/>
    <arg name="map_file_path" value="$(arg geotiff_map_file_path)"/>
  </include>

</launch>

When in command prompt and I enter roslaunch hector_slam_launch tutorial.launch* , the Hector slam starts up In RViz along with all the necessary values in the command prompt but it also comes up with errors.

edit retag flag offensive close merge delete