I cannot get my code to get the parameters on my config file. I just do like it is on the tutorials but it does not work and I do not understand why. I believe it might be a namespace problem with rospy. I have never had this problem with C++.
My Github with the whole code: https://github.com/renanmb/boat_pid_controller
throttle_interpolator.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import Float64
from boat_controller.msg import Drive
# import some utils.
import numpy as np
import copy as copy
class InterpolateThrottle:
def __init__(self):
self.rpm_input_topic = rospy.get_param('~rpm_input_topic', '/vesc/commands/motor/unsmoothed_speed')
self.rpm_output_left_topic = rospy.get_param('~rpm_output_left_topic', '/vesc/commands/motor/speed_left')
self.rpm_output_right_topic = rospy.get_param('~rpm_output_right_topic', '/vesc/commands/motor/speed_right')
self.servo_input_topic = rospy.get_param('~servo_input_topic', '/vesc/commands/servo/unsmoothed_position')
self.servo_output_topic = rospy.get_param('~servo_output_topic', '/vesc/commands/servo/position')
self.max_rpm_acceleration = rospy.get_param('/vesc/max_rpm_acceleration')
self.max_rpm = rospy.get_param('/vesc/vesc_driver/speed_max')
self.min_rpm = rospy.get_param('/vesc/vesc_driver/speed_min')
self.throttle_smoother_rate = rospy.get_param('/vesc/throttle_smoother_rate')
self.rpm_to_erpm_gain = rospy.get_param('/vesc/rpm_to_erpm_gain')
self.max_servo_speed = rospy.get_param('/vesc/max_servo_speed')
self.steering_angle_to_servo_gain = rospy.get_param('/vesc/steering_angle_to_servo_gain')
self.servo_smoother_rate = rospy.get_param('/vesc/servo_smoother_rate')
self.max_servo = rospy.get_param('/vesc/vesc_driver/servo_max')
self.min_servo = rospy.get_param('/vesc/vesc_driver/servo_min')
self.last_rpm = 0
self.desired_rpm = self.last_rpm
self.last_servo = rospy.get_param('/vesc/steering_angle_to_servo_offset')
self.desired_servo_position = self.last_servo
self.rpm_output_left = rospy.Publisher(self.rpm_output_left_topic, Float64,queue_size=1)
self.rpm_output_right = rospy.Publisher(self.rpm_output_right_topic, Float64,queue_size=1)
self.servo_output = rospy.Publisher(self.servo_output_topic, Float64,queue_size=1)
rospy.Subscriber(self.rpm_input_topic, Drive, self._process_throttle_command)
rospy.Subscriber(self.servo_input_topic, Float64, self._process_servo_command)
self.max_delta_servo = abs(self.steering_angle_to_servo_gain * self.max_servo_speed / self.servo_smoother_rate)
rospy.Timer(rospy.Duration(1.0/self.servo_smoother_rate), self._publish_servo_command)
self.max_delta_rpm = abs(self.rpm_to_erpm_gain * self.max_rpm_acceleration / self.throttle_smoother_rate)
rospy.Timer(rospy.Duration(1.0/self.max_delta_rpm), self._publish_throttle_left_command)
rospy.Timer(rospy.Duration(1.0/self.max_delta_rpm), self._publish_throttle_right_command)
self._run()
def _run(self):
rospy.spin()
def _publish_throttle_left_command(self, evt):
desired_delta = self.desired_rpm_left-self.last_rpm_left
clipped_delta = max(min(desired_delta, self.max_delta_rpm), -self.max_delta_rpm)
smoothed_rpm = self.last_rpm_left + clipped_delta
self.last_rpm_left = smoothed_rpm
self.rpm_output_left.publish(Float64(smoothed_rpm))
def _publish_throttle_right_command(self, evt):
desired_delta = self.desired_rpm_right-self.last_rpm_right
clipped_delta = max(min(desired_delta, self.max_delta_rpm), -self.max_delta_rpm)
smoothed_rpm = self.last_rpm_right + clipped_delta
self.last_rpm_right = smoothed_rpm
self.rpm_output_right.publish(Float64(smoothed_rpm))
def _process_throttle_command(self,msg):
input_rpm_left = msg.data
input_rpm_right = msg.data
input_rpm_left = min(max(input_rpm_left, self.min_rpm), self.max_rpm)
input_rpm_right = min(max(input_rpm_right, self.min_rpm), self.max_rpm)
self.desired_rpm_left = input_rpm_left
self.desired_rpm_right = input_rpm_right
def _publish_servo_command(self, evt):
desired_delta = self.desired_servo_position-self.last_servo
clipped_delta = max(min(desired_delta, self.max_delta_servo), -self.max_delta_servo)
smoothed_servo = self.last_servo + clipped_delta
self.last_servo = smoothed_servo
self.servo_output.publish(Float64(smoothed_servo))
def _process_servo_command(self,msg):
input_servo = msg.data
input_servo = min(max(input_servo, self.min_servo), self.max_servo)
self.desired_servo_position = input_servo
if __name__ == '__main__':
try:
rospy.init_node('Throttle_Interpolator')
p = InterpolateThrottle()
rospy.spin()
except rospy.ROSInterruptException:
pass
Error message:
Traceback (most recent call last):
File "/home/renan/rostest_ws/src/boat_controller/src/throttle_interpolator.py", line 136, in <module>
p = InterpolateThrottle()
File "/home/renan/rostest_ws/src/boat_controller/src/throttle_interpolator.py", line 28, in __init__
self.max_rpm_acceleration = rospy.get_param('/vesc/max_rpm_acceleration')
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/client.py", line 465, in get_param
return _param_server[param_name] #MasterProxy does all the magic for us
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/msproxy.py", line 123, in __getitem__
raise KeyError(key)
KeyError: '/vesc/max_rpm_acceleration'
Originally posted by renanmb on ROS Answers with karma: 33 on 2018-10-10
Post score: 0
Original comments
Comment by aPonza on 2018-10-10:
Is anybody actually loading the parameters from the yaml in the parameter server? Do you have other nodes not in the repo setting them?
If not, you could read them in?
Comment by renanmb on 2018-10-10:
My other nodes loads its parameters just fine. I tried the command rosparam load and it does not work. It says the file does not exist.