thrusterTest.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import rospy
4 import actionlib
5 
6 from std_msgs.msg import Header, Float32MultiArray, Int16MultiArray
7 from riptide_controllers.msg import ThrusterTestAction
8 
9 import yaml
10 import numpy as np
11 
12 NEUTRAL_PWM = 1500
13 POSITIVE_PWM = 1550
14 NEGATIVE_PWM = 1450
15 
16 class ThrusterTest(object):
17  THRUSTER_PERCENT = 0.05
18 
19  def __init__(self):
20  self.pwm_pub = rospy.Publisher("command/pwm", Int16MultiArray, queue_size=5)
21 
22  # Get the mass and COM
23  with open(rospy.get_param('~vehicle_config'), 'r') as stream:
24  self.vehicle_file = yaml.safe_load(stream)
25  self.num_thrusters = len(self.vehicle_file["thrusters"])
26  self.max_force = self.vehicle_file["thruster"]["max_force"]
27 
28  self._as = actionlib.SimpleActionServer("thruster_test", ThrusterTestAction, execute_cb=self.execute_cb, auto_start=False)
29  self._as.start()
30 
31  def publish_pwm(self, pwm):
32  msg = Int16MultiArray()
33  msg.data = list(pwm.astype(int))
34  self.pwm_pub.publish(msg)
35 
36  def execute_cb(self, goal):
37  rospy.loginfo("Starting ThrusterTest Action")
38  pwm = np.zeros(self.num_thrusters) + NEUTRAL_PWM
39  while True:
40  for i in range(self.num_thrusters):
41  if self._as.is_preempt_requested():
42  rospy.loginfo('Preempted ThrusterTest Action')
43  self.publish_pwm(np.zeros(self.num_thrusters) + NEUTRAL_PWM)
44  self._as.set_preempted()
45  return
46 
47  thruster_type = self.vehicle_file["thrusters"][i]["type"]
48 
49  if thruster_type == 0:
50  pwm[i] = POSITIVE_PWM
51  else:
52  pwm[i] = NEGATIVE_PWM
53 
54  for _ in range(30):
55  self.publish_pwm(pwm)
56  rospy.sleep(0.1)
57  pwm[i] = NEUTRAL_PWM
58 
59 
60 
61  #should never reach this point in the code
62  rospy.loginfo("ThrustTest succeeded")
63  self._as.set_succeeded()
64 
65 if __name__ == '__main__':
66  rospy.init_node('thruster_test')
67  server = ThrusterTest()
68  rospy.spin()
thrusterTest.ThrusterTest._as
_as
Definition: thrusterTest.py:28
thrusterTest.ThrusterTest.max_force
max_force
Definition: thrusterTest.py:26
thrusterTest.ThrusterTest.execute_cb
def execute_cb(self, goal)
Definition: thrusterTest.py:36
thrusterTest.ThrusterTest
Definition: thrusterTest.py:16
thrusterTest.ThrusterTest.vehicle_file
vehicle_file
Definition: thrusterTest.py:24
thrusterTest.ThrusterTest.pwm_pub
pwm_pub
Definition: thrusterTest.py:20
thrusterTest.ThrusterTest.__init__
def __init__(self)
Definition: thrusterTest.py:19
actionlib::SimpleActionServer
thrusterTest.ThrusterTest.num_thrusters
num_thrusters
Definition: thrusterTest.py:25
thrusterTest.ThrusterTest.publish_pwm
def publish_pwm(self, pwm)
Definition: thrusterTest.py:31


riptide_controllers
Author(s): Blaine Miller, Mitchell Sayre
autogenerated on Wed Mar 2 2022 00:50:23