forked from sasilva1998/uPyArlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arlorobot.py
185 lines (129 loc) · 4.38 KB
/
arlorobot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# pins and serial library
from machine import UART
import utime
# object to control de DHB-10 driver
class ArloRobot(object):
# com packet sending
def com(self, packet):
for i in packet:
self.uart.write(i)
self.uart.write(" ")
self.uart.write("\r")
tinit=utime.ticks_ms()
resp=''
while (utime.ticks_ms()-tinit)<150: #timeout of 1600us
data=self.uart.read(1)
if data is not None and data!=b'\r':
resp=resp+str(data)[2:][:-1]
if resp is not None:
resp=resp.split("xd6")[-1].split("xc3")[-1].split(" ")
try:
resp=[int(i) for i in resp]
except:
return None
if len(resp)!=2:
return resp[0]
return resp
return resp
# set up/set down
# serialid is defined as the ID of the serial bus from the
# microcontroller, however tx and rx can be defined
def __init__(self,serialid=2,tx=17,rx=16,baudrate=19200):
self.tx=tx
self.rx=rx
self.baudrate=baudrate
self.uart=UART(serialid,self.baudrate)
self.uart.init(self.baudrate, bits=8, parity=None, stop=1, txbuf=0,tx=self.tx, rx=self.rx)
self.com(["TXPIN","CH2"])#needed so that reading is possible
self.com(["DEC"])
self.com(["ECHO","ON"])
# end serial connection
def end(self):
self.uart.deinit()
#-------------------------- movements methods------------------------
# Turn command
# motor_movements corresponds to the amount of encode positions
# top_speed to the positions per second
def turn(self, motor_movement, top_speed):
self.com(["TURN",str(motor_movement),str(top_speed)])
# arc turns the motors so that the platform moves along the arc of a circle
# of a given radius with a speed and an angle
def arc(self, radius, top_speed, angle):
self.com(["ARC",str(radius),str(top_speed),str(angle)])
# left/right -> -32767 to 32767
# speed -> 1 to 32767
def move(self, left, right, speed):
self.com(["MOVE",str(left),str(right),str(speed)])
# left/right -> -32767 to 32767
def go_speed(self, left, right):
self.com(["GOSPD",str(left),str(right)])
# left/right -> -127 to 127
def go(self, left, right):
self.com(["GO",str(left),str(right)])
def travel(self,distance,top_speed,angle):
self.com(["TRVL",str(distance),str(top_speed),str(angle)])
#--------------------------- information methods -----------------------
def read_left_counts(self):
return self.com(["DIST"])[0]
def read_right_counts(self):
return self.com(["DIST"])[1]
def read_left_speed(self):
return self.com(["SPD"])[0]
def read_right_speed(self):
return self.com(["SPD"])[1]
def read_head_angle(self):
return self.com(["HEAD"])[0]
def read_firmware_ver(self):
return self.com(["VER"])
def read_hardware_ver(self):
return self.com(["HWVER"])
def clear_counts(self):
return self.com(["RST"])
# ---------------------------- communication modes -----------------------
def write_pulse_mode(self):
self.com(["PULSE"])
def set_lf_mode(self,status):
return self.com(["SETLF",str(status)])
def set_hex_com(self):
return self.com(['HEX'])
def set_dec_com(self):
return self.com(['DEC'])
def set_echo_mode(self,status):
return self.com(["ECHO",str(status)])
def set_verbose_mode(self,status):
return self.com(["VERB",str(status)])
def set_rx_pin(self,pin):
return self.com(["RXPIN",str(pin)])
def set_tx_pin(self,pin):
return self.com(["TXPIN",str(pin)])
def set_baud_rate(self,baud):
return self.com(["BAUD",str(baud)])
def set_pwm_scale(self,scale):
return self.com(["SCALE",str(scale)])
def set_pace(self,pace):
return self.com(["PACE",str(pace)])
def set_hold(self,hold):
return self.com(["HOLD",str(baud)])
#-------------------------- closed loop constants ----------------------
def set_ki_limit(self, limit):
return self.com(["KIP",str(limit)])
def set_ki_decay(self, decay):
return self.com(["KIT",str(decay)])
def set_kimax(self,maxim):
return self.com(["KIMAX",str(maxim)])
def set_ki_constant(self,constant):
return self.com(["KI",str(constant)])
def set_kp_constant(self,constant):
return self.com(["KP",str(constant)])
def set_acc_rate(self,acc):
return self.com(["ACC",str(acc)])
def set_ramp_rate(self,rate):
return self.com(["RAMP",str(rate)])
def set_live_zone(self,limit):
return self.com(["LZ",str(limit)])
def set_dead_zone(self,limit):
return self.com(["DZ",str(limit)])
def set_ppr(self,ppr):
return self.com(["PPR",str(ppr)])
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))