-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotor_controller.py
66 lines (46 loc) · 1.68 KB
/
motor_controller.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
#!/usr/bin/env python
import rospy
import serial
from geometry_msgs.msg import Twist
VMAX = 10
OMEGAMAX = 2
class drive():
def __init__(self):
self.vel_sub = rospy.Subscriber("cmd_vel", Twist, self.cmd_cb)
self.rate = rospy.Rate(100)
self.my_serial = serial.Serial('/dev/ttyACM0',115200,timeout=1)
def cmd_cb(self, data):
self.v = data.linear.x
self.omega = data.angular.z
left_wheel = self.v + self.omega
right_wheel = self.v - self.omega
if (left_wheel > 0):
left_direc = 1
left_speed = self.mymap(left_wheel, 0, VMAX+OMEGAMAX, 10, 50)
else:
left_direc = 0
left_speed = self.mymap(-left_wheel, 0, VMAX+OMEGAMAX, 10, 50)
if (right_wheel > 0):
right_direc = 1
right_speed = self.mymap(right_wheel, 0, VMAX+OMEGAMAX, 10, 50)
else:
right_direc = 0
right_speed = self.mymap(-right_wheel, 0, VMAX+OMEGAMAX, 10, 50)
if (self.v == 0 and self.omega == 0):
right_wheel = 0
left_wheel = 0
# first send command for left wheels
command = '0' + str(left_direc) + str(left_speed)
command = int(bin(int(command)).replace("0b",""))
self.my_serial.write(command)
# right wheels
command = '1' + str(right_direc) + str(right_speed)
command = int(bin(int(command)).replace("0b",""))
self.my_serial.write(command)
self.rate.sleep()
def mymap(self,c,a,b,d,e):
return d + (c-a)*(e-d)/(b-a)
if __name__ == "__main__":
rospy.init_node("motor_controller")
chal_jaa = drive()
rospy.spin()