speed_sine_wave.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import rospy
3 import math
4 from dataspeed_ulc_can import Speed
5 from dataspeed_ulc_msgs.msg import UlcReport
6 
7 
8 class SpeedSineWave(Speed):
9  APPROACHING = 0
10  TRACKING = 1
11 
12  def __init__(self):
13  rospy.init_node('speed_sine_wave')
14  super(SpeedSineWave, self).__init__()
15 
16  self.speed_meas = 0
18  self.state = self.APPROACHING
19  rospy.Subscriber('/vehicle/ulc_report', UlcReport, self.recv_report)
20 
21  def timer_callback(self, event):
22 
23  if not self.enabled:
24  self.t = 0
25  self.state = self.APPROACHING
26  return
27 
28  if self.state == self.APPROACHING:
29  self.ulc_cmd.linear_velocity = self.v1
30  self.t = 0
31  if abs(self.ulc_cmd.linear_velocity - self.speed_meas) < 0.4 and self.reached_target_stamp < 0:
32  self.reached_target_stamp = event.current_real.to_sec()
33 
34  # Wait 3 seconds before starting the sine wave input
35  if self.reached_target_stamp > 0 and (event.current_real.to_sec() - self.reached_target_stamp) > 3:
36  self.state = self.TRACKING
37  self.reached_target_stamp = -1
38 
39  elif self.state == self.TRACKING:
40  amplitude = 0.5 * (self.v2 - self.v1)
41  offset = 0.5 * (self.v2 + self.v1)
42  self.ulc_cmd.linear_velocity = offset - amplitude * math.cos(2 * math.pi / self.period * self.t)
43  self.t += 0.02
44 
45  self.pub_ulc_cmd.publish(self.ulc_cmd)
46  if self.v1 == 0 and self.v2 == 0:
47  rospy.logwarn_throttle(1.0, 'both speed targets are zero')
48 
49  def recv_report(self, msg):
50  self.speed_meas = msg.speed_meas
51 
52 
53 if __name__ == '__main__':
54  try:
55  node_instance = SpeedSineWave()
56  rospy.spin()
57  except rospy.ROSInterruptException:
58  pass


dataspeed_ulc_can
Author(s): Micho Radovnikovich
autogenerated on Thu Jul 9 2020 03:45:43