-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent_optical_flow_node.py
executable file
·113 lines (85 loc) · 3.73 KB
/
student_optical_flow_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
from __future__ import division
import rospy
import numpy as np
from geometry_msgs.msg import TwistStamped
from raspicam_node.msg import MotionVectors
import numpy as np
import rospy
import tf
from sensor_msgs.msg import Imu, Range
from std_msgs.msg import Empty
class OpticalFlowNode(object):
"""
Subscribe to the optical flow vectors and publish linear velocity as a Twist message.
"""
def __init__(self, node_name):
rospy.init_node(node_name)
# flow variables
camera_wh = (320, 240)
self.max_flow = camera_wh[0] / 16.0 * camera_wh[1] / 16.0 * 2**7
self.flow_scale = .165
self.flow_coeff = 100 * self.flow_scale / self.max_flow # (multiply by 100 for cm to m conversion)
self.altitude = 0.03 # initialize to a bit off the ground
self.altitude_ts = rospy.Time.now()
# subscribers
self._sub_mv = rospy.Subscriber('/raspicam_node/motion_vectors', MotionVectors, self.motion_cb, queue_size=1)
self._sub_alt = rospy.Subscriber('/pidrone/range', Range, self.altitude_cb, queue_size=1)
self.setup()
def setup(self):
# ROS setup:
############
# Publisher:
# TODO: create a ROS publisher to publish the velocities
# message type: TwistStamped
# topic: /pidrone/picamera/twist
# note: ensure that you pass in the argument queue_size=1 to the
# publisher to avoid lag
self.twistpub = rospy.Publisher('/pidrone/picamera/twist', TwistStamped, queue_size=1)
# Subscriber:
# TODO: subscribe to /pidrone/range to extract altitude (z position) for
# scaling
# message type: Range
# callback method: altitude_cb
self._sub_alt = rospy.Subscriber('/pidrone/range', Range, self.altitude_cb, queue_size=1)
# TODO: subscribe to /raspicam_node/motion_vectors to extract the flow vectors for estimating velocity.
# message type: MotionVectors
# callback method: motion_cb
self._sub_mv = rospy.Subscriber('/raspicam_node/motion_vectors', MotionVectors, self.motion_cb, queue_size=1)
def motion_cb(self, msg):
''' Average the motion vectors and publish the
twist message that is the average of all the vectors..
'''
# signed 1-byte values
x = msg.x
y = msg.y
# calculate the planar and yaw motions
# TODO: calculate the optical flow velocities by summing the flow vectors
opflow_x = np.sum(x)
opflow_y = np.sum(y)
x_motion = opflow_x * self.flow_coeff * self.altitude
y_motion = opflow_y * self.flow_coeff * self.altitude
# TODO: Create a TwistStamped message, fill in the values you've calculated,
# and publish this using the publisher you've created in setup
twist_msg = TwistStamped()
twist_msg.header.stamp = rospy.Time.now()
twist_msg.twist.linear.x = x_motion
twist_msg.twist.linear.y = -y_motion
self.twistpub.publish(twist_msg)
duration_from_last_altitude = rospy.Time.now() - self.altitude_ts
if duration_from_last_altitude.to_sec() > 10:
rospy.logwarn("No altitude received for {:10.4f} seconds.".format(duration_from_last_altitude.to_sec()))
# TODO: Implement this method
def altitude_cb(self, msg):
"""
The altitude of the robot
Args:
msg: the message publishing the altitude
"""
self.altitude = msg.range
self.altitude_ts = msg.header.stamp
def main():
optical_flow_node = OpticalFlowNode("optical_flow_node")
rospy.spin()
if __name__ == '__main__':
main()