-
Notifications
You must be signed in to change notification settings - Fork 2
/
edubit.py
198 lines (166 loc) · 4.46 KB
/
edubit.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
186
187
188
189
190
191
192
193
194
195
196
197
198
from microbit import *
from utime import sleep_ms
I2C_ADDRESS = 0x08
REG_ADD_REVISION = 0
REG_ADD_SERVO_1 = 1
REG_ADD_SERVO_2 = 2
REG_ADD_SERVO_3 = 3
REG_ADD_M1A = 4
REG_ADD_M1B = 5
REG_ADD_M2A = 6
REG_ADD_M2B = 7
REG_ADD_LB_UTH = 8
REG_ADD_LB_LTH = 9
REG_ADD_OV_TH = 10
REG_ADD_VIN = 11
REG_ADD_PWR_STATE = 12
REG_ADD_LB_STATE = 13
REG_ADD_OV_STATE = 14
Servo_S1 = REG_ADD_SERVO_1
Servo_S2 = REG_ADD_SERVO_2
Servo_S3 = REG_ADD_SERVO_3
Servo_All = 1000
Motor_M1 = 0
Motor_M2 = 1
Motor_All = 1000
Direction_Forward = 0
Direction_Backward = 1
LED_Red = pin14
LED_Yellow = pin15
LED_Green = pin16
LED_All = 1000
SOUND_BIT_PIN = pin1
POTENTIO_BIT_PIN = pin2
IR_BIT_PIN = pin8
flag = 0
def limit(value,min,max):
if value < min:
value = min
elif value > max:
value = max
return value
def i2cRead(register):
buf = bytearray(1)
buf[0] = register
value = 0
i2c.write(I2C_ADDRESS,buf,True)
value = i2c.read(I2C_ADDRESS,1)
#value = int.from_bytes(value, "little")
return value[0]
def i2cWrite(register,data):
buffer = bytearray(2)
buffer[0] = register
buffer[1] = data
i2c.write(I2C_ADDRESS,buffer)
def power_monitor():
global flag
global oldPowerState
if flag == 0:
oldPowerState = is_power_on()
flag = 1
if is_power_on():
if oldPowerState == False:
brake_motor(Motor_M1)
brake_motor(Motor_M2)
disable_servo(Servo_S1)
disable_servo(Servo_S2)
disable_servo(Servo_S3)
reset()
oldPowerState = True
else:
oldPowerState = False
sleep_ms(200)
def brake_motor(motorChannel):
if motorChannel == Motor_M1:
i2cWrite(REG_ADD_M1A, 0)
i2cWrite(REG_ADD_M1B, 0)
elif motorChannel == Motor_M2:
i2cWrite(REG_ADD_M2A, 0)
i2cWrite(REG_ADD_M2B, 0)
elif motorChannel == Motor_All:
i2cWrite(REG_ADD_M1A, 0)
i2cWrite(REG_ADD_M1B, 0)
i2cWrite(REG_ADD_M2A, 0)
i2cWrite(REG_ADD_M2B, 0)
def run_motor(motorChannel,direction,speed):
speed = limit(speed,0,255)
if motorChannel == Motor_M1:
if direction == Direction_Forward:
i2cWrite(REG_ADD_M1A,speed)
i2cWrite(REG_ADD_M1B,0)
else:
i2cWrite(REG_ADD_M1A,0)
i2cWrite(REG_ADD_M1B,speed)
elif motorChannel == Motor_M2:
if direction == Direction_Forward:
i2cWrite(REG_ADD_M2A,speed)
i2cWrite(REG_ADD_M2B,0)
else:
i2cWrite(REG_ADD_M2A,0)
i2cWrite(REG_ADD_M2B,speed)
elif motorChannel == Motor_All:
if direction == Direction_Forward:
i2cWrite(REG_ADD_M1A,speed)
i2cWrite(REG_ADD_M1B,0)
i2cWrite(REG_ADD_M2A,speed)
i2cWrite(REG_ADD_M2B,0)
else:
i2cWrite(REG_ADD_M1A,0)
i2cWrite(REG_ADD_M1B,speed)
i2cWrite(REG_ADD_M2A,0)
i2cWrite(REG_ADD_M2B,speed)
def disable_servo(servo):
if servo == Servo_All:
i2cWrite(Servo_S1,0)
i2cWrite(Servo_S2,0)
i2cWrite(Servo_S3,0)
else:
i2cWrite(servo,0)
def sets_servo_position(servo,position):
position = limit(position,0,180)
pulseWidth = int(position * 20 / 18 + 50)
if servo == Servo_All:
i2cWrite(Servo_S1,pulseWidth)
i2cWrite(Servo_S2,pulseWidth)
i2cWrite(Servo_S3,pulseWidth)
else:
i2cWrite(servo,pulseWidth)
def is_power_on():
if i2cRead(REG_ADD_PWR_STATE) != 0:
return True
else:
return False
def is_low_batt():
if i2cRead(REG_ADD_LB_STATE) != 0:
return True
else:
return False
def is_overvoltage():
if i2cRead(REG_ADD_OV_STATE) != 0:
return True
else:
return False
def read_Vin():
return i2cRead(REG_ADD_VIN) / 10
def set_led(color,state):
if color == LED_Red:
LED_Red.write_digital(state)
if color == LED_Yellow:
LED_Yellow.write_digital(state)
if color == LED_Green:
LED_Green.write_digital(state)
if color == LED_All:
LED_Red.write_digital(state)
LED_Yellow.write_digital(state)
LED_Green.write_digital(state)
def read_sound_sensor():
return SOUND_BIT_PIN.read_analog()
def read_pot_value():
return POTENTIO_BIT_PIN.read_analog()
def read_IR_sensor():
return IR_BIT_PIN.read_digital()
def is_IR_sensor_triggered():
if IR_BIT_PIN.read_digital() != 0:
return True
else:
return False