-
Notifications
You must be signed in to change notification settings - Fork 30
/
bioloid.py
89 lines (71 loc) · 2.12 KB
/
bioloid.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
#
# Test file to send some commands to a bioloid servo
#
import pyb
import stm
# Bioloid command to set the led on on servo 1
# Sending WRITE to ID 1 offset 0x19 len 1
# W: 0000: ff ff 01 04 03 19 01 dd
# R: 0000: ff ff 01 02 00 fc
# Rcvd Status: None
#
# Bioloid command to set the led off on servo 1
# Sending WRITE to ID 1 offset 0x19 len 1
# W: 0000: ff ff 01 04 03 19 00 de
# R: 0000: ff ff 01 02 00 fc
# Rcvd Status: None
#
# The checksum for a packet is the sum of the characters in the packet
# (which starts after the second 0xff) and then bitwise inverted.
def checksum(packet):
return ~sum(packet[2:len(packet)-1]) & 0xff
def fill_checksum(packet):
packet[len(packet) - 1] = checksum(packet)
def dump(label, packet):
print(label, ' '.join('{:02x}'.format(x) for x in packet))
def pkt_led_on(servo_id):
packet = bytearray((0xff, 0xff, servo_id, 4, 3, 0x19, 1, 0))
fill_checksum(packet)
return packet
def pkt_led_off(servo_id):
packet = bytearray((0xff, 0xff, servo_id, 4, 3, 0x19, 0, 0))
fill_checksum(packet)
return packet
def enable_rx():
"""Set the RE bit in the CR1 register."""
cr1 = stm.mem16[stm.USART6 + stm.USART_CR1]
cr1 |= 0x04
stm.mem16[stm.USART6 + stm.USART_CR1] = cr1
def disable_rx():
"""Clear the RE bit in the CR1 register."""
cr1 = stm.mem16[stm.USART6 + stm.USART_CR1]
cr1 &= ~0x04
stm.mem16[stm.USART6 + stm.USART_CR1] = cr1
def led_on():
disable_rx()
u6.write(pkt_led_on(12))
enable_rx()
def led_off():
disable_rx()
u6.write(pkt_led_off(12))
enable_rx()
def process_char():
while True:
char = u6.readchar()
if char == -1:
return
print('Got {:02x}'.format(char))
u6 = pyb.UART(6, 1000000)
# Turn on HDSEL - which puts the UART in half-duplex mode. This connects
# Rx to Tx internally, and only enables the transmitter when there is data
# to send.
cr3 = stm.mem16[stm.USART6 + stm.USART_CR3]
cr3 |= 0x08
stm.mem16[stm.USART6 + stm.USART_CR3] = cr3
for i in range(4):
print('on')
led_on()
process_char()
print('off')
led_off()
process_char()