-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuggyMove.py
More file actions
62 lines (62 loc) · 1.83 KB
/
buggyMove.py
File metadata and controls
62 lines (62 loc) · 1.83 KB
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
from microbit import i2c, sleep
import math
CHIP_ADDR=0x62
MODE_1_REG_ADDR=0x00
MODE_2_REG_ADDR=0x01
MOTOR_OUT_ADDR=0x08
MODE_1_REG_VALUE=0x00
MODE_2_REG_VALUE=0x04
MOTOR_OUT_VALUE=0xAA
LEFT_MOTOR=0x04
RIGHT_MOTOR=0x02
class MOVEMotor:
def __init__(self):
buffer=bytearray(2)
buffer[0]=MODE_1_REG_ADDR
buffer[1]=MODE_1_REG_VALUE
i2c.write(CHIP_ADDR,buffer,False)
buffer[0]=MODE_2_REG_ADDR
buffer[1]=MODE_2_REG_VALUE
i2c.write(CHIP_ADDR,buffer,False)
buffer[0]=MOTOR_OUT_ADDR
buffer[1]=MOTOR_OUT_VALUE
i2c.write(CHIP_ADDR,buffer,False)
def setLeftMotorSpeed(self,speed):
motorBuffer=bytearray(2)
gndPinBuffer=bytearray(2)
if math.fabs(speed)>255: motorBuffer[1]=255
else: motorBuffer[1]=int(math.fabs(speed))
gndPinBuffer[1]=0x00
if speed>0:
motorBuffer[0]=LEFT_MOTOR
gndPinBuffer[0]=LEFT_MOTOR +1
else:
motorBuffer[0]=LEFT_MOTOR +1
gndPinBuffer[0]=LEFT_MOTOR
i2c.write(CHIP_ADDR,motorBuffer,False)
i2c.write(CHIP_ADDR,gndPinBuffer,False)
def setRightMotorSpeed(self,speed):
motorBuffer=bytearray(2)
gndPinBuffer=bytearray(2)
if math.fabs(speed)>255: motorBuffer[1]=255
else: motorBuffer[1]=int(math.fabs(speed))
gndPinBuffer[1]=0x00
if speed>0:
motorBuffer[0]=RIGHT_MOTOR +1
gndPinBuffer[0]=RIGHT_MOTOR
else:
motorBuffer[0]=RIGHT_MOTOR
gndPinBuffer[0]=RIGHT_MOTOR +1
i2c.write(CHIP_ADDR,motorBuffer,False)
i2c.write(CHIP_ADDR,gndPinBuffer,False)
def stopMotors(self):
stopBuffer=bytearray(2)
stopBuffer[0]=LEFT_MOTOR
stopBuffer[1]=0x00
i2c.write(CHIP_ADDR,stopBuffer,False)
stopBuffer[0]=LEFT_MOTOR +1
i2c.write(CHIP_ADDR,stopBuffer,False)
stopBuffer[0]=RIGHT_MOTOR
i2c.write(CHIP_ADDR,stopBuffer,False)
stopBuffer[0]=RIGHT_MOTOR +1
i2c.write(CHIP_ADDR,stopBuffer,False)