@tigerpython/robotics-libraries 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG +68 -0
- package/LICENSE +373 -0
- package/README.md +81 -0
- package/calliope/README.md +15 -0
- package/calliope/callibot.py +176 -0
- package/calliope/callibotmot.py +46 -0
- package/calliope/callimk.py +143 -0
- package/calliope/cbalarm.py +14 -0
- package/calliope/cpglow.py +76 -0
- package/calliope/cpmike.py +16 -0
- package/calliope/cprover.py +33 -0
- package/calliope/cputils.py +23 -0
- package/calliope/libraries.json +10 -0
- package/calliope/libraries.raw.json +10 -0
- package/calliope/min/callibot.py +97 -0
- package/calliope/min/callibotmot.py +23 -0
- package/calliope/min/callimk.py +47 -0
- package/calliope/min/cbalarm.py +6 -0
- package/calliope/min/cpglow.py +29 -0
- package/calliope/min/cpmike.py +8 -0
- package/calliope/min/cprover.py +9 -0
- package/calliope/min/cputils.py +7 -0
- package/dist/index.d.mts +124 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +2896 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2862 -0
- package/dist/index.mjs.map +1 -0
- package/microbit/README.md +48 -0
- package/microbit/controller.py +212 -0
- package/microbit/huskylens.py +479 -0
- package/microbit/libraries.json +19 -0
- package/microbit/libraries.raw.json +19 -0
- package/microbit/mbalarm.py +15 -0
- package/microbit/mbbitbot.py +127 -0
- package/microbit/mbglow.py +77 -0
- package/microbit/mbled.py +56 -0
- package/microbit/mbmarsrover.py +216 -0
- package/microbit/mbminibit.py +139 -0
- package/microbit/mbrobot.py +401 -0
- package/microbit/mbrobot_legacy.py +90 -0
- package/microbit/mbrobot_plus.py +179 -0
- package/microbit/mbrobot_plusV2.py +490 -0
- package/microbit/mbrobot_plusV3.py +427 -0
- package/microbit/mbrobotmot.py +49 -0
- package/microbit/mbthetabot.py +167 -0
- package/microbit/mbwait.py +50 -0
- package/microbit/mbxgo.py +173 -0
- package/microbit/min/controller.py +42 -0
- package/microbit/min/huskylens.py +145 -0
- package/microbit/min/mbalarm.py +6 -0
- package/microbit/min/mbbitbot.py +40 -0
- package/microbit/min/mbglow.py +29 -0
- package/microbit/min/mbled.py +18 -0
- package/microbit/min/mbmarsrover.py +62 -0
- package/microbit/min/mbminibit.py +50 -0
- package/microbit/min/mbrobot.py +82 -0
- package/microbit/min/mbrobot_legacy.py +45 -0
- package/microbit/min/mbrobot_plus.py +75 -0
- package/microbit/min/mbrobot_plusV2.py +102 -0
- package/microbit/min/mbrobot_plusV3.py +194 -0
- package/microbit/min/mbrobotmot.py +25 -0
- package/microbit/min/mbthetabot.py +47 -0
- package/microbit/min/mbwait.py +23 -0
- package/microbit/min/mbxgo.py +37 -0
- package/package.json +54 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
from microbit import *
|
|
2
|
+
|
|
3
|
+
_speed = 50
|
|
4
|
+
_XGOInit = False
|
|
5
|
+
_TX = pin14
|
|
6
|
+
_RX = pin13
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def checkInit(func):
|
|
10
|
+
def wrapper(*args, **kwargs):
|
|
11
|
+
global _XGOInit
|
|
12
|
+
if not _XGOInit:
|
|
13
|
+
init_xgo_serial(_TX, _RX)
|
|
14
|
+
return func(*args, **kwargs)
|
|
15
|
+
return wrapper
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _map(speed, in_min, in_max, out_min, out_max):
|
|
19
|
+
return (speed - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@checkInit
|
|
23
|
+
def _move(direction, speed):
|
|
24
|
+
move_buffer = bytearray(9)
|
|
25
|
+
move_buffer[0] = 0x55
|
|
26
|
+
move_buffer[1] = 0x00
|
|
27
|
+
move_buffer[2] = 0x09
|
|
28
|
+
move_buffer[3] = 0x00
|
|
29
|
+
move_buffer[7] = 0x00
|
|
30
|
+
move_buffer[8] = 0xAA
|
|
31
|
+
|
|
32
|
+
speed = max(0, min(100, speed))
|
|
33
|
+
|
|
34
|
+
if direction == 0:
|
|
35
|
+
move_buffer[4] = 0x30
|
|
36
|
+
move_buffer[5] = _map(speed, 0, 100, 128, 255)
|
|
37
|
+
elif direction == 1:
|
|
38
|
+
move_buffer[4] = 0x30
|
|
39
|
+
move_buffer[5] = _map(speed, 0, 100, 128, 0)
|
|
40
|
+
elif direction == 2:
|
|
41
|
+
move_buffer[4] = 0x31
|
|
42
|
+
move_buffer[5] = _map(speed, 0, 100, 128, 0)
|
|
43
|
+
elif direction == 3:
|
|
44
|
+
move_buffer[4] = 0x31
|
|
45
|
+
move_buffer[5] = _map(speed, 0, 100, 128, 255)
|
|
46
|
+
|
|
47
|
+
move_buffer[6] = ~(0x09 + 0x00 + move_buffer[4] + move_buffer[5]) & 0xFF
|
|
48
|
+
|
|
49
|
+
uart.write(move_buffer)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@checkInit
|
|
53
|
+
def clampX(milimeters=50):
|
|
54
|
+
clampBuffer = bytearray(9)
|
|
55
|
+
clampBuffer[0] = 0x55
|
|
56
|
+
clampBuffer[1] = 0x00
|
|
57
|
+
clampBuffer[2] = 0x09
|
|
58
|
+
clampBuffer[3] = 0x00
|
|
59
|
+
clampBuffer[4] = 0x73
|
|
60
|
+
clampBuffer[7] = 0x00
|
|
61
|
+
clampBuffer[8] = 0xAA
|
|
62
|
+
|
|
63
|
+
clampBuffer[5] = milimeters
|
|
64
|
+
clampBuffer[6] = ~(0x09 + 0x00 + 0x73 + clampBuffer[5]) & 0xFF
|
|
65
|
+
|
|
66
|
+
uart.write(clampBuffer)
|
|
67
|
+
sleep(1000)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@checkInit
|
|
71
|
+
def clampZ(milimeters=50):
|
|
72
|
+
clampBuffer = bytearray(9)
|
|
73
|
+
clampBuffer[0] = 0x55
|
|
74
|
+
clampBuffer[1] = 0x00
|
|
75
|
+
clampBuffer[2] = 0x09
|
|
76
|
+
clampBuffer[3] = 0x00
|
|
77
|
+
clampBuffer[4] = 0x74
|
|
78
|
+
clampBuffer[7] = 0x00
|
|
79
|
+
clampBuffer[8] = 0xAA
|
|
80
|
+
|
|
81
|
+
clampBuffer[5] = milimeters
|
|
82
|
+
clampBuffer[6] = ~(0x09 + 0x00 + 0x74 + clampBuffer[5]) & 0xFF
|
|
83
|
+
|
|
84
|
+
uart.write(clampBuffer)
|
|
85
|
+
sleep(1000)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@checkInit
|
|
89
|
+
def clamp(force):
|
|
90
|
+
clampBuffer = bytearray(9)
|
|
91
|
+
clampBuffer[0] = 0x55
|
|
92
|
+
clampBuffer[1] = 0x00
|
|
93
|
+
clampBuffer[2] = 0x09
|
|
94
|
+
clampBuffer[3] = 0x00
|
|
95
|
+
clampBuffer[4] = 0x71
|
|
96
|
+
clampBuffer[7] = 0x00
|
|
97
|
+
clampBuffer[8] = 0xAA
|
|
98
|
+
|
|
99
|
+
clampBuffer[5] = force
|
|
100
|
+
clampBuffer[6] = ~(0x09 + 0x00 + 0x71 + clampBuffer[5]) & 0xFF
|
|
101
|
+
|
|
102
|
+
uart.write(clampBuffer)
|
|
103
|
+
sleep(1000)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def init_xgo_serial(tx_pin, rx_pin, baudrate=115200):
|
|
107
|
+
global _XGOInit
|
|
108
|
+
uart.init(baudrate=baudrate, tx=tx_pin, rx=rx_pin)
|
|
109
|
+
init_action()
|
|
110
|
+
_XGOInit = True
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def init_action():
|
|
114
|
+
commands_buffer = bytearray(9)
|
|
115
|
+
commands_buffer[0] = 0x55
|
|
116
|
+
commands_buffer[1] = 0x00
|
|
117
|
+
commands_buffer[2] = 0x09
|
|
118
|
+
commands_buffer[3] = 0x00
|
|
119
|
+
commands_buffer[4] = 0x3E
|
|
120
|
+
commands_buffer[5] = 0xFF
|
|
121
|
+
commands_buffer[6] = ~(0x09 + 0x00 + 0x3E + 0xFF) & 0xFF
|
|
122
|
+
commands_buffer[7] = 0x00
|
|
123
|
+
commands_buffer[8] = 0xAA
|
|
124
|
+
|
|
125
|
+
uart.write(commands_buffer)
|
|
126
|
+
sleep(2000)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@checkInit
|
|
130
|
+
def action(id):
|
|
131
|
+
commands_buffer = bytearray(9)
|
|
132
|
+
commands_buffer[0] = 0x55
|
|
133
|
+
commands_buffer[1] = 0x00
|
|
134
|
+
commands_buffer[2] = 0x09
|
|
135
|
+
commands_buffer[3] = 0x00
|
|
136
|
+
commands_buffer[4] = 0x3E
|
|
137
|
+
commands_buffer[5] = id
|
|
138
|
+
commands_buffer[6] = ~(0x09 + 0x00 + 0x3E + id) & 0xFF
|
|
139
|
+
commands_buffer[7] = 0x00
|
|
140
|
+
commands_buffer[8] = 0xAA
|
|
141
|
+
|
|
142
|
+
uart.write(commands_buffer)
|
|
143
|
+
sleep(2000)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def changeInit(tx, rx):
|
|
147
|
+
global _TX
|
|
148
|
+
global _RX
|
|
149
|
+
_TX = tx
|
|
150
|
+
_RX = rx
|
|
151
|
+
init_xgo_serial(_TX, _RX)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@checkInit
|
|
155
|
+
def setSpeed(speed):
|
|
156
|
+
global _speed
|
|
157
|
+
_speed = speed
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def forward():
|
|
161
|
+
_move(0, _speed)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def backward():
|
|
165
|
+
_move(1, _speed)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def left():
|
|
169
|
+
_move(2, _speed)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def right():
|
|
173
|
+
_move(3, _speed)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
_B=False
|
|
2
|
+
_A=1.
|
|
3
|
+
from microbit import run_every,pin13,pin14,pin15,pin16,pin8,pin1,pin2,pin12,button_a,button_b,sleep
|
|
4
|
+
class _Controller_Button:
|
|
5
|
+
def __init__(A,pin):B=pin;A._pin=B;A.previous_state=0;A._press_count=0;A._pressed_before=_B;B.set_pull(B.PULL_UP)
|
|
6
|
+
def _f3(A):
|
|
7
|
+
B=1-A._pin.read_digital()
|
|
8
|
+
if B==1 and A.previous_state==0:A._press_count+=1;A._pressed_before=True
|
|
9
|
+
A.previous_state=B
|
|
10
|
+
def is_pressed(A):return _B if A._pin.read_digital()else True
|
|
11
|
+
def was_pressed(A):B=A._pressed_before;A._pressed_before=_B;return B
|
|
12
|
+
def get_presses(A):B=A._press_count;A._press_count=0;return B
|
|
13
|
+
class _Controller_Analog_Stick:
|
|
14
|
+
def __init__(A,pinX,pinY,pinZ):A.pin_x=pinX;A.pin_y=pinY;A.button_z=_Controller_Button(pinZ);A.dead_zone=.01;A.center_x=0;A.center_y=0;A.min_x=-1;A.max_x=1;A.min_y=-1;A.max_y=1
|
|
15
|
+
def calibrate(A,dead_zone,center_x=.0,center_y=.0,x_min=-_A,x_max=_A,y_min=-_A,y_max=_A):A.dead_zone=min(.2,max(.01,dead_zone));A.center_x=min(.4,max(-.4,center_x));A.center_y=min(.5,max(-.5,center_y));A.min_x=min(-.5,max(-_A,x_min));A.max_x=min(_A,max(.5,x_max));A.min_y=min(-.5,max(-_A,y_min));A.max_y=min(_A,max(.5,y_max))
|
|
16
|
+
def _f5(C,value,center,v_min,v_max):
|
|
17
|
+
B=center;A=value-_A-B
|
|
18
|
+
if abs(A)-C.dead_zone/2.>.0:
|
|
19
|
+
if A<.0:A=A/abs(v_min-B)
|
|
20
|
+
else:A=A/abs(v_max-B)
|
|
21
|
+
A=min(_A,max(-_A,A));A=.0 if abs(A)<=C.dead_zone else A;return A
|
|
22
|
+
def get_x(A):B=A.pin_x.read_analog()/512.;C=A._f5(B,A.center_x,A.min_x,A.max_x);return C
|
|
23
|
+
def get_y(A):B=A.pin_y.read_analog()/512.;C=A._f5(B,A.center_y,A.min_y,A.max_y);return C
|
|
24
|
+
def get_z(A):return 1 if A.button_z.is_pressed()else 0
|
|
25
|
+
def is_pressed(A):return A.button_z.is_pressed()
|
|
26
|
+
def was_pressed(A):return A.button_z.was_pressed()
|
|
27
|
+
def get_presses(A):return A.button_z.get_presses()
|
|
28
|
+
def vibrate(state):pin12.write_digital(state)
|
|
29
|
+
joystick=_Controller_Analog_Stick(pin1,pin2,pin8)
|
|
30
|
+
button_z=joystick.button_z
|
|
31
|
+
button_c=_Controller_Button(pin13)
|
|
32
|
+
button_green=button_c
|
|
33
|
+
button_d=_Controller_Button(pin14)
|
|
34
|
+
button_yellow=button_d
|
|
35
|
+
button_e=_Controller_Button(pin15)
|
|
36
|
+
button_red=button_e
|
|
37
|
+
button_f=_Controller_Button(pin16)
|
|
38
|
+
button_blue=button_f
|
|
39
|
+
trigger_left=button_a
|
|
40
|
+
trigger_right=button_b
|
|
41
|
+
def _f1():global button_c;global button_d;global button_e;global button_f;global joystick;button_c._f3();button_d._f3();button_e._f3();button_f._f3();joystick.button_z._f3()
|
|
42
|
+
run_every(_f1,days=0,h=0,min=0,s=0,ms=33)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
_E='Invalid model_id. Must be number in range [0,4]'
|
|
2
|
+
_D='Error: ID must be in range from 1 to 255.'
|
|
3
|
+
_C=None
|
|
4
|
+
_B=True
|
|
5
|
+
_A=False
|
|
6
|
+
from microbit import i2c,sleep,running_time
|
|
7
|
+
import math
|
|
8
|
+
_g1=['FaceRecognition','ObjectTracking','ObjectRecognition','LineTracking','ColorRecognition','TagRecognition','ObjectClassification','QRRecognition','BarcodeRecognition']
|
|
9
|
+
class Request_Command:KNOCK=44;ALGORITHM=45;ALL=32;BLOCKS=33;BLOCKS_LEARNED=36;BLOCKS_OF_ID=39;ARROWS=34;ARROWS_LEARNED=37;ARROWS_OF_ID=40;LEARNED=35;ALL_OF_ID=38;LEARN=54;FORGET=55;CUSTOM_LABEL=47;CUSTOM_TEXT=52;CLEAR_TEXT=53;SAVE_MODEL=50;LOAD_MODEL=51;SAVE_PHOTO=48;SAVE_SCREENSHOT=57;IS_PRO=59;VERSION=60
|
|
10
|
+
class Return_Code:ANY=1;OK=46;BUSY=61;INFO=41;BLOCK=42;ARROW=43;IS_PRO=59;NEED_PRO=62
|
|
11
|
+
class Algorithm:FACE_RECOGNITION=0;OBJECT_TRACKING=1;OBJECT_RECOGNITION=2;LINE_TRACKING=3;COLOR_RECOGNITION=4;TAG_RECOGNITION=5;OBJECT_CLASSIFICATION=6;QR_RECOGNITION=7;BARCODE_RECOGNITION=8
|
|
12
|
+
class Block:
|
|
13
|
+
def __init__(A,x,y,width,height,id):A.x=x;A.y=y;A.width=width;A.height=height;A.id=id
|
|
14
|
+
def _f2(A):return'Block: ID_'+str(A.id)+' Pos: ('+str(A.x)+' '+str(A.y)+') Size: ('+str(A.width)+' '+str(A.height)+')'
|
|
15
|
+
class Arrow:
|
|
16
|
+
def __init__(A,x_tail,y_tail,x_head,y_head,id):A.x_tail=x_tail;A.y_tail=y_tail;A.x_head=x_head;A.y_head=y_head;A.id=id
|
|
17
|
+
def get_direction(A):
|
|
18
|
+
C=A.x_head-A.x_tail;D=A.y_head-A.y_tail;B=90-math.degrees(math.atan2(D,C))
|
|
19
|
+
if B<0:B=B+360
|
|
20
|
+
return int(B)
|
|
21
|
+
def _f2(A):return'Arrow: ID_'+str(A.id)+' ('+str(A.x_tail)+' '+str(A.y_tail)+')->('+str(A.x_head)+' '+str(A.y_head)+')'
|
|
22
|
+
def byte_checksum(byte_list):return sum(byte_list)&255
|
|
23
|
+
def hexify(byte_array):
|
|
24
|
+
A=byte_array
|
|
25
|
+
if len(A)==0:return''
|
|
26
|
+
return'0x'+''.join('{:02x}'.format(A)for A in A)
|
|
27
|
+
class Huskylens:
|
|
28
|
+
I2C_ADDR=50
|
|
29
|
+
def __init__(A):A.learned_slot_count=0;A.id_slots={};A.id_names={};A.algorithm=Algorithm.OBJECT_TRACKING;A.clear_texts();A.pro_enabled=A.is_pro()
|
|
30
|
+
def initialize(A):
|
|
31
|
+
B=_A
|
|
32
|
+
for E in range(5):
|
|
33
|
+
A.knock();B,F=A.get_response(Return_Code.OK)
|
|
34
|
+
if B:break
|
|
35
|
+
if B>0:
|
|
36
|
+
C=A.clear_texts();D=A.set_algorithm(Algorithm.OBJECT_TRACKING)
|
|
37
|
+
if C and D:print('Initialization successful!');return _B
|
|
38
|
+
else:print("Initialization Failed. Couldn't change Algorithm");return _A
|
|
39
|
+
else:print('Initialization Failed. Please check connection to Huskylens.');return _A
|
|
40
|
+
def send_request(D,command,data=_C):
|
|
41
|
+
B=data;A=bytearray(b'U\xaa\x11\x00\x00');A[3]=0 if B is _C else len(B);A[4]=command
|
|
42
|
+
if B:
|
|
43
|
+
for C in B:A.append(C)
|
|
44
|
+
A.append(byte_checksum(A));i2c.write(Huskylens.I2C_ADDR,A);sleep(50)
|
|
45
|
+
def get_response(K,return_code=Return_Code.ANY,timeout=500):
|
|
46
|
+
C=return_code;A=bytearray(b'U\x00\x00\x00\x00');I=running_time()
|
|
47
|
+
while running_time()-I<timeout:
|
|
48
|
+
D=i2c.read(Huskylens.I2C_ADDR,1)[0]
|
|
49
|
+
if D==85:break
|
|
50
|
+
if D!=85:return-1,[]
|
|
51
|
+
for J in range(4):A[J+1]=i2c.read(Huskylens.I2C_ADDR,1)[0]
|
|
52
|
+
if A[0:3]!=b'U\xaa\x11':return-2,[]
|
|
53
|
+
E=A[3];F=A[4];B=[]
|
|
54
|
+
if E>0:G=i2c.read(Huskylens.I2C_ADDR,E+1);B=G[0:-1];H=G[-1]
|
|
55
|
+
else:H=ord(i2c.read(Huskylens.I2C_ADDR,1))
|
|
56
|
+
if H!=byte_checksum(list(A)+B):return-3,[]
|
|
57
|
+
if C==Return_Code.ANY or F==C:return F,B
|
|
58
|
+
else:return 0,B
|
|
59
|
+
def knock(A):A.send_request(Request_Command.KNOCK)
|
|
60
|
+
def set_algorithm(B,algorithm):
|
|
61
|
+
A=algorithm
|
|
62
|
+
if(A==Algorithm.QR_RECOGNITION or A==Algorithm.BARCODE_RECOGNITION)and not B.pro_enabled:raise RuntimeError('Error: Huskylens PRO version is required for algorithm ',_g1[A]);return _A
|
|
63
|
+
D=[A,0];B.send_request(Request_Command.ALGORITHM,D);C,E=B.get_response(Return_Code.OK)
|
|
64
|
+
if C>0:print('Current Algorithm:',_g1[A]);B.algorithm=A
|
|
65
|
+
return _B if C>0 else _A
|
|
66
|
+
def get_all(A):return A._f7(Request_Command.ALL)
|
|
67
|
+
def get_all_learned(A):return A._f7(Request_Command.LEARNED)
|
|
68
|
+
def get_all_with_id(A,id):
|
|
69
|
+
if id<=0 or id>255:raise RuntimeError(_D)
|
|
70
|
+
return A._f7(Request_Command.ALL_OF_ID,id)
|
|
71
|
+
def get_one(A):B=A._f7(Request_Command.ALL);return A._f8(B)
|
|
72
|
+
def get_one_learned(A):B=A._f7(Request_Command.LEARNED);return A._f8(B)
|
|
73
|
+
def get_one_with_id(A,id):
|
|
74
|
+
if id<=0 or id>255:raise RuntimeError(_D)
|
|
75
|
+
B=A._f7(Request_Command.ALL_OF_ID,id);return A._f8(B)
|
|
76
|
+
def attach_label(A,id,name):
|
|
77
|
+
B=name
|
|
78
|
+
if A.algorithm==Algorithm.OBJECT_TRACKING or A.algorithm==Algorithm.LINE_TRACKING:C=A._f6(1,B)
|
|
79
|
+
elif A.algorithm==Algorithm.FACE_RECOGNITION or A.algorithm==Algorithm.TAG_RECOGNITION or A.algorithm==Algorithm.OBJECT_CLASSIFICATION or A.algorithm==Algorithm.OBJECT_RECOGNITION:
|
|
80
|
+
D=A.id_slots.get(id)
|
|
81
|
+
if D==_C:raise RuntimeError("Can't attach a name to an unlearned ID number")
|
|
82
|
+
A.id_names[id]=B;C=_B
|
|
83
|
+
for E in D:F=A._f6(E,B);C=C and F>0
|
|
84
|
+
else:A.id_names[id]=B;C=A._f6(id,B)
|
|
85
|
+
return _B if C>0 else _A
|
|
86
|
+
def clear_labels(A):
|
|
87
|
+
A.id_names.clear()
|
|
88
|
+
for B in range(10):A._f6(B,'')
|
|
89
|
+
def add_text(E,text,position_x,position_y):
|
|
90
|
+
C=position_y;B=position_x;D=bytes(text,'utf-8')
|
|
91
|
+
if len(D)>19:raise RuntimeError('Custom Text must be less than 20 bytes long.')
|
|
92
|
+
if B>300 or B<0 or C<35 or C>240:raise RuntimeError("Custom Text can't be placed outside of screen pixel size.")
|
|
93
|
+
A=[len(D)];A.append(255 if B>255 else 0);A.append(B%255);A.append(240-C);A.extend(list(D));E.send_request(Request_Command.CUSTOM_TEXT,A);F,G=E.get_response(Return_Code.OK);return _B if F>0 else _A
|
|
94
|
+
def clear_texts(A):A.send_request(Request_Command.CLEAR_TEXT);B,C=A.get_response(Return_Code.OK);return _B if B>0 else _A
|
|
95
|
+
def learn(A,id,name=_C):
|
|
96
|
+
if id<=0 or id>255:raise RuntimeError('Parameter ID for learned item must be in range [0,255]')
|
|
97
|
+
if A.algorithm==Algorithm.OBJECT_TRACKING or A.algorithm==Algorithm.LINE_TRACKING:id=1
|
|
98
|
+
C=500
|
|
99
|
+
if A.algorithm==Algorithm.OBJECT_CLASSIFICATION:C=1000
|
|
100
|
+
A.send_request(Request_Command.LEARN,[id,0]);B,E=A.get_response(Return_Code.OK,C)
|
|
101
|
+
if B>0:
|
|
102
|
+
A.learned_slot_count+=1
|
|
103
|
+
if not id in A.id_slots:A.id_slots[id]=[A.learned_slot_count]
|
|
104
|
+
else:A.id_slots[id].append(A.learned_slot_count)
|
|
105
|
+
D=A.id_names.get(id)
|
|
106
|
+
if D!=_C:B=A.attach_label(A.learned_slot_count,D)
|
|
107
|
+
elif name!=_C:B=A.attach_label(id,name)
|
|
108
|
+
return _B if B>0 else _A
|
|
109
|
+
def forget(A):
|
|
110
|
+
A.send_request(Request_Command.FORGET);B,C=A.get_response(Return_Code.OK)
|
|
111
|
+
if B:A.learned_slot_count=0;A.id_slots.clear()
|
|
112
|
+
return _B if B else _A
|
|
113
|
+
def save_photo(A):A.send_request(Request_Command.SAVE_PHOTO);B,C=A.get_response(Return_Code.OK,1000);return _B if B>0 else _A
|
|
114
|
+
def save_screenshot(A):A.send_request(Request_Command.SAVE_SCREENSHOT);B,C=A.get_response(Return_Code.OK,1000);return _B if B>0 else _A
|
|
115
|
+
def save_model(B,model_id):
|
|
116
|
+
A=model_id
|
|
117
|
+
if A<0 or A>4:raise RuntimeError(_E)
|
|
118
|
+
B.send_request(Request_Command.SAVE_MODEL,[A,0]);C,D=B.get_response(Return_Code.OK,1000);print('Model saving: Check Huskylens screen for Result!\n\tModel name:',_g1[B.algorithm]+'_Backup_'+str(A)+'.conf');return _B if C>0 else _A
|
|
119
|
+
def load_model(B,model_id):
|
|
120
|
+
A=model_id
|
|
121
|
+
if A<0 or A>4:raise RuntimeError(_E)
|
|
122
|
+
B.send_request(Request_Command.LOAD_MODEL,[A,0]);C,D=B.get_response(Return_Code.OK,1000);print('Model Loading: Check Huskylens screen for Result!');return _B if C>0 else _A
|
|
123
|
+
def is_pro(A):A.send_request(Request_Command.IS_PRO);B,C=A.get_response(Return_Code.IS_PRO);return bool(C[0])if B>0 else _A
|
|
124
|
+
def _f6(C,id,name):
|
|
125
|
+
A=bytes(name,'utf-8')
|
|
126
|
+
if len(A)>19:raise RuntimeError('Custom Name must be less than 20 bytes long.')
|
|
127
|
+
B=[id,len(A)+1];B.extend(list(A));B.append(0);C.send_request(Request_Command.CUSTOM_LABEL,B);D,E=C.get_response(Return_Code.OK);return _B if D>0 else _A
|
|
128
|
+
def _f7(B,request_command,id=-1):
|
|
129
|
+
I=_C if id<0 else[id,0];B.send_request(request_command,I);G,H=B.get_response(Return_Code.INFO)
|
|
130
|
+
if not G:raise RuntimeError('Failed to request results. Got answer:'+str(G))
|
|
131
|
+
J=H[0]+H[1]*255;C=0;D=[]
|
|
132
|
+
while C<J:
|
|
133
|
+
E,A=B.get_response(Return_Code.ANY)
|
|
134
|
+
if E==Return_Code.BLOCK and B.algorithm!=3:K=A[0]+A[1]*255;L=240-A[2]+A[3]*255;M=A[4]+A[5]*255;N=A[6]+A[7]*255;F=A[8];O=Block(K,L,M,N,F);D.append(O);C+=1
|
|
135
|
+
elif E==Return_Code.ARROW:P=A[0]+A[1]*255;Q=240-A[2]+A[3]*255;R=A[4]+A[5]*255;S=240-A[6]+A[7]*255;F=A[8];T=Arrow(P,Q,R,S,F);D.append(T);C+=1
|
|
136
|
+
elif E==0:return[]
|
|
137
|
+
return D
|
|
138
|
+
def _f8(E,results):
|
|
139
|
+
C=_C;D=440
|
|
140
|
+
for A in results:
|
|
141
|
+
B=0
|
|
142
|
+
if E.algorithm==Algorithm.LINE_TRACKING:B=abs(A.x_tail+(A.x_tail-A.x_head)//2-160)+abs(A.y_tail+(A.y_tail-A.y_head)//2-120)
|
|
143
|
+
else:B=abs(A.x-160)+abs(A.y-120)
|
|
144
|
+
if B<D:C,D=A,B
|
|
145
|
+
return C
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from microbit import*
|
|
2
|
+
from neopixel import*
|
|
3
|
+
from utime import ticks_us,sleep_us
|
|
4
|
+
_g1=200
|
|
5
|
+
_g2=12
|
|
6
|
+
_g3=NeoPixel(pin13,_g2)
|
|
7
|
+
_g4=0
|
|
8
|
+
_g5=0
|
|
9
|
+
def w(leftforward,leftbackward,rightforward,rightbackward):
|
|
10
|
+
if leftforward>0 or rightforward>0:pin16.write_analog(leftforward+_g4);pin8.write_analog(leftbackward);pin14.write_analog(rightforward-_g4);pin12.write_analog(rightbackward)
|
|
11
|
+
elif leftbackward>0 or rightbackward>0:pin16.write_analog(leftforward);pin8.write_analog(leftbackward+_g4);pin14.write_analog(rightforward);pin12.write_analog(rightbackward-_g4)
|
|
12
|
+
elif leftforward==0 and rightforward==0 and leftbackward==0 and rightbackward==0:pin16.write_analog(0);pin8.write_analog(0);pin14.write_analog(0);pin12.write_analog(0)
|
|
13
|
+
def forward():w(_g1,0,_g1,0)
|
|
14
|
+
def backward():w(0,_g1,0,_g1)
|
|
15
|
+
def stop():w(0,0,0,0)
|
|
16
|
+
def right():w(0,_g1,_g1,0)
|
|
17
|
+
def left():w(_g1,0,0,_g1)
|
|
18
|
+
def set_led(pos,red,green,blue):_g3[pos]=red,green,blue;_g3.show()
|
|
19
|
+
def fill(red,green,blue):
|
|
20
|
+
for i in range(_g2):_g3[i]=red,green,blue
|
|
21
|
+
_g3.show()
|
|
22
|
+
def getDistance():
|
|
23
|
+
pin15.write_digital(1);sleep_us(10);pin15.write_digital(0);pin15.set_pull(pin15.NO_PULL)
|
|
24
|
+
while pin15.read_digital()==0:0
|
|
25
|
+
start=ticks_us()
|
|
26
|
+
while pin15.read_digital()==1:0
|
|
27
|
+
end=ticks_us();echo=end-start;distance=int(.01715*echo);return distance
|
|
28
|
+
def calibrate(offset,differential=0):global _g4;global _g5;global _g8;_g5=max(min(int(offset),50),-50);_g4=max(min(int(differential),50),-50);_g8=max(min(arcScaling,50),-50);setSpeed(_g1/1023*100)
|
|
29
|
+
def setSpeed(percent):global _g1;speed=percent/100*1023;speed+=_g5/100*1023;speed=min(max(speed,0),1023);_g1=speed
|
|
30
|
+
def getLine(bit):
|
|
31
|
+
mask=1<<bit;value=0
|
|
32
|
+
try:value=i2c.read(28,1)[0]
|
|
33
|
+
except OSError:pass
|
|
34
|
+
if value&mask>0:return 1
|
|
35
|
+
else:return 0
|
|
36
|
+
def getLight(index):
|
|
37
|
+
if index==0:return pin1.read_analog()
|
|
38
|
+
elif index==1:return pin2.read_analog()
|
|
39
|
+
def leftArc(radius):speed=_g1;inner_wheel_speed=speed*(radius/(radius+1));w(inner_wheel_speed,0,speed,0)
|
|
40
|
+
def rightArc(radius):speed=_g1;inner_wheel_speed=speed*(radius/(radius+1));w(speed,0,inner_wheel_speed,0)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from microbit import*
|
|
2
|
+
_g1=0
|
|
3
|
+
_g2=0
|
|
4
|
+
_g3=0
|
|
5
|
+
_g4=50
|
|
6
|
+
_g5=True
|
|
7
|
+
_g6=False
|
|
8
|
+
def makeGlow():global _g6;display.set_pixel(2,2,9);_g6=True
|
|
9
|
+
def clear():display.clear()
|
|
10
|
+
def forward():_f1(1)
|
|
11
|
+
def back():_f1(-1)
|
|
12
|
+
def right(angle):global _g3;_g3=(_g3+angle)%360
|
|
13
|
+
def left(angle):right(-angle)
|
|
14
|
+
def setPos(x,y):global _g1,_g2;_g1=x;_g2=y;_f2()
|
|
15
|
+
def getPos():return _g1,_g2
|
|
16
|
+
def setSpeed(speed):global _g4;_g4=speed
|
|
17
|
+
def showTrace(enable):global _g5;_g5=enable
|
|
18
|
+
def isLit():return display.get_pixel(_g1+2,4-(_g2+2))==9
|
|
19
|
+
def _f1(s):
|
|
20
|
+
global _g1,_g2;sleep(2000-_g4*20);d=_g3//45
|
|
21
|
+
if d in[1,2,3]:_g1+=s
|
|
22
|
+
if d in[5,6,7]:_g1-=s
|
|
23
|
+
if d in[0,1,7]:_g2+=s
|
|
24
|
+
if d in[3,4,5]:_g2-=s
|
|
25
|
+
_f2()
|
|
26
|
+
def _f2():
|
|
27
|
+
if not _g6:print('Use "makeGlow()" to create a Glow.');raise Exception('Glow not initialized.')
|
|
28
|
+
if not _g5:display.clear()
|
|
29
|
+
if-2<=_g1<=2 and-2<=_g2<=2:display.set_pixel(_g1+2,4-(_g2+2),9)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from neopixel import*
|
|
2
|
+
import gc
|
|
3
|
+
from microbit import pin2
|
|
4
|
+
_g1=24
|
|
5
|
+
_g2=NeoPixel(pin2,_g1)
|
|
6
|
+
def fill(red,green,blue):
|
|
7
|
+
for i in range(_g1):_g2[i]=red,green,blue
|
|
8
|
+
_g2.show()
|
|
9
|
+
def set_led(pos,red,green,blue):_g2[pos]=red,green,blue;_g2.show()
|
|
10
|
+
def clear():_g2.clear()
|
|
11
|
+
def shift_by(amount):
|
|
12
|
+
shifted_copy=[None]*_g1
|
|
13
|
+
for n in range(0,_g1):next_i=(n+amount)%_g1;shifted_copy[n]=_g2[next_i]
|
|
14
|
+
for n in range(0,_g1):_g2[n]=shifted_copy[n]
|
|
15
|
+
_g2.show()
|
|
16
|
+
def lerp_RGB(r1,g1,b1,r2,g2,b2,percent):
|
|
17
|
+
if percent<.0 or percent>1e2:raise RuntimeError("Argument 'percent' must be between 0 and 100.")
|
|
18
|
+
red=max(0,min(255,round(r1+(r2-r1)*(percent/1e2))));green=max(0,min(255,round(g1+(g2-g1)*(percent/1e2))));blue=max(0,min(255,round(b1+(b2-b1)*(percent/1e2))));return red,green,blue
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from microbit import*
|
|
2
|
+
from neopixel import*
|
|
3
|
+
import utime
|
|
4
|
+
_g1=[0,9,11,13,15]
|
|
5
|
+
_g2=[0,0,0,0,0]
|
|
6
|
+
_g3=False
|
|
7
|
+
_g4=4
|
|
8
|
+
_g5=NeoPixel(pin2,_g4)
|
|
9
|
+
_g6=50
|
|
10
|
+
_g7=40
|
|
11
|
+
_g8=40
|
|
12
|
+
_g9=0
|
|
13
|
+
_g10=0
|
|
14
|
+
_g11=0
|
|
15
|
+
_g12=500
|
|
16
|
+
_g13=29.1
|
|
17
|
+
def _f1(dirL,powerL,dirR,powerR):pinsL=pin1,pin12;pinsR=pin8,pin0;pinsL[dirL].write_analog(powerL);pinsL[1-dirL].write_analog(0);pinsR[dirR].write_analog(powerR);pinsR[1-dirR].write_analog(0)
|
|
18
|
+
def _f2(side,direction,power):
|
|
19
|
+
if side==0:pins=pin1,pin12
|
|
20
|
+
elif side==1:pins=pin8,pin0
|
|
21
|
+
pins[direction].write_analog(power);pins[1-direction].write_analog(0)
|
|
22
|
+
def _f3(r):
|
|
23
|
+
outerSpeed=_g6;rCm=int(r*100);threshold=outerSpeed-max(rCm+20,40)
|
|
24
|
+
if threshold<=0:outerSpeed=min(max(rCm+40,40),100)
|
|
25
|
+
reducedSpeed=0
|
|
26
|
+
if rCm>=4:flattening=(100-outerSpeed)//2;reducedSpeed=(rCm*10-35)/(rCm*(11+(_g11-4)/10)+90+flattening);reducedSpeed=reducedSpeed*outerSpeed
|
|
27
|
+
innerByte=_f6(int(reducedSpeed),0);outerByte=_f6(int(outerSpeed),0);return innerByte,outerByte
|
|
28
|
+
def _f4():global _g3;_g3=True;i2cData=bytearray(2);i2cData[0]=0;i2cData[1]=16;i2c.write(64,i2cData);i2cData[0]=254;i2cData[1]=101;i2c.write(64,i2cData);i2cData[0]=0;i2cData[1]=129;i2c.write(64,i2cData)
|
|
29
|
+
def _f5(pin,value,timeout):
|
|
30
|
+
start_time=utime.ticks_us()
|
|
31
|
+
while pin.read_digital()!=value:
|
|
32
|
+
if utime.ticks_diff(utime.ticks_us(),start_time)>timeout:return 0
|
|
33
|
+
start_time=utime.ticks_us()
|
|
34
|
+
while pin.read_digital()==value:
|
|
35
|
+
if utime.ticks_diff(utime.ticks_us(),start_time)>timeout:return 0
|
|
36
|
+
return utime.ticks_diff(utime.ticks_us(),start_time)
|
|
37
|
+
def calibrate(offset,differential=0,arcScaling=0):global _g10;global _g9;global _g11;_g9=max(min(int(offset),500),-50);_g10=max(min(int(differential),150),-150);_g11=max(min(arcScaling,50),-15);setSpeed(_g6)
|
|
38
|
+
def setSpeed(speed):
|
|
39
|
+
global _g6;global _g7;global _g8;_g6=int(min(max(speed,0),100));powerValue=_f6(_g6,_g9);boost=round((1-_g6/100)*abs(_g10))if _g6>0 else 0;reduction=round(_g6/100*abs(_g10))
|
|
40
|
+
if _g10>0:_g7=powerValue-reduction;_g8=powerValue+boost
|
|
41
|
+
else:_g7=powerValue+boost;_g8=powerValue-reduction
|
|
42
|
+
def _f6(speed,offset):analogValue=int(speed*1023/100)+offset;return min(max(analogValue,0),1023)
|
|
43
|
+
def stop():_f1(0,0,0,0)
|
|
44
|
+
def forward():_f1(0,_g7,0,_g8)
|
|
45
|
+
def backward():_f1(1,_g7,1,_g8)
|
|
46
|
+
def setServo(servoNum,angle):
|
|
47
|
+
global _g1,_g3
|
|
48
|
+
if _g3==False:_f4()
|
|
49
|
+
offsetNum=servoNum;servoNum=_g1[servoNum];i2cData=bytearray(2);start=0;angle=max(min(angle,90),-90);stop=396+(angle+_g2[offsetNum])*223/90;i2cData[0]=6+servoNum*4+2;i2cData[1]=int(stop)+255;i2c.write(64,i2cData);i2cData[0]=6+servoNum*4+3;i2cData[1]=int(stop)>>8;i2c.write(64,i2cData)
|
|
50
|
+
def clearServos():setServo(0,0);setServo(1,0);setServo(2,0);setServo(3,0);setServo(4,0);sleep(500)
|
|
51
|
+
def steer(angle):angle=max(min(angle,45),-45);setServo(1,angle);setServo(2,-angle);setServo(3,-angle);setServo(4,angle);sleep(500)
|
|
52
|
+
def setLED(pos,red,green,blue):_g5[pos]=red,green,blue;_g5.show()
|
|
53
|
+
def fill(red,green,blue):
|
|
54
|
+
for i in range(_g4):_g5[i]=red,green,blue
|
|
55
|
+
_g5.show()
|
|
56
|
+
def getDistance():
|
|
57
|
+
trig=pin13;echo=pin13;d=10;trig.set_pull(trig.NO_PULL)
|
|
58
|
+
for _ in range(10):
|
|
59
|
+
trig.write_digital(0);utime.sleep_us(2);trig.write_digital(1);utime.sleep_us(10);trig.write_digital(0);duration=_f5(echo,1,_g12*_g13)
|
|
60
|
+
if duration>0:d=duration;break
|
|
61
|
+
return round(d/_g13)
|
|
62
|
+
def calibrateServo(pos,angleDifferential):global _g2;angleDifferential=max(min(angleDifferential,90),-90);_g2[pos]=angleDifferential;clearServos()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from microbit import*
|
|
2
|
+
from neopixel import*
|
|
3
|
+
import utime
|
|
4
|
+
_g1=4
|
|
5
|
+
_g2=NeoPixel(pin13,_g1)
|
|
6
|
+
_g3=50
|
|
7
|
+
_g4=90
|
|
8
|
+
_g5=90
|
|
9
|
+
_g6=0
|
|
10
|
+
_g7=0
|
|
11
|
+
_g8=0
|
|
12
|
+
_g9=500
|
|
13
|
+
_g10=29.1
|
|
14
|
+
def _f1(dirL,powerL,dirR,powerR):pin8.write_analog(powerL if dirL==1 else 0);pin12.write_analog(0 if dirL==1 else powerL);pin16.write_analog(0 if dirR==1 else powerR);pin14.write_analog(powerR if dirR==1 else 0)
|
|
15
|
+
def _f2(pin,value,timeout):
|
|
16
|
+
start_time=utime.ticks_us()
|
|
17
|
+
while pin.read_digital()!=value:
|
|
18
|
+
if utime.ticks_diff(utime.ticks_us(),start_time)>timeout:return 0
|
|
19
|
+
start_time=utime.ticks_us()
|
|
20
|
+
while pin.read_digital()==value:
|
|
21
|
+
if utime.ticks_diff(utime.ticks_us(),start_time)>timeout:return 0
|
|
22
|
+
return utime.ticks_diff(utime.ticks_us(),start_time)
|
|
23
|
+
def _f3(r):
|
|
24
|
+
outerSpeed=_g3
|
|
25
|
+
if r>0:innerSpeed=outerSpeed*max(.2,min(1,1-r/100))
|
|
26
|
+
else:innerSpeed=0
|
|
27
|
+
innerByte=_f4(int(innerSpeed),0);outerByte=_f4(int(outerSpeed),0);return innerByte,outerByte
|
|
28
|
+
def _f4(speed,offset):analogValue=int(speed*255/100)+offset;return min(max(analogValue,0),255)
|
|
29
|
+
def calibrate(offset,differential=0,arcScaling=0):global _g7;global _g6;global _g8;_g6=max(min(int(offset),100),-10);_g7=max(min(int(differential),150),-150);_g8=max(min(arcScaling,50),-15);setSpeed(_g3)
|
|
30
|
+
def setSpeed(speed):
|
|
31
|
+
global _g3;global _g4;global _g5;_g3=int(min(max(speed,0),100));powerByte=_f4(_g3,_g6);boost=round((1-_g3/100)*abs(_g7))if _g3>0 else 0;reduction=round(_g3/100*abs(_g7))
|
|
32
|
+
if _g7>0:_g4=powerByte-reduction;_g5=powerByte+boost
|
|
33
|
+
else:_g4=powerByte+boost;_g5=powerByte-reduction
|
|
34
|
+
def stop():_f1(0,0,0,0)
|
|
35
|
+
def forward():_f1(0,_g4,0,_g5)
|
|
36
|
+
def backward():_f1(1,_g4,1,_g5)
|
|
37
|
+
def left():_f1(1,_g4,0,_g5)
|
|
38
|
+
def right():_f1(0,_g4,1,_g5)
|
|
39
|
+
def rightArc(radius):inner,outer=_f3(radius);_f1(0,outer,0,inner)
|
|
40
|
+
def leftArc(radius):inner,outer=_f3(radius);_f1(0,inner,0,outer)
|
|
41
|
+
def setLED(pos,red,green,blue):_g2[pos]=red,green,blue;_g2.show()
|
|
42
|
+
def fill(red,green,blue):
|
|
43
|
+
for i in range(_g1):_g2[i]=red,green,blue
|
|
44
|
+
_g2.show()
|
|
45
|
+
def getDistance():
|
|
46
|
+
trig=pin15;echo=pin15;d=10;trig.set_pull(trig.NO_PULL)
|
|
47
|
+
for _ in range(10):
|
|
48
|
+
trig.write_digital(0);utime.sleep_us(2);trig.write_digital(1);utime.sleep_us(10);trig.write_digital(0);duration=_f2(echo,1,_g9*_g10)
|
|
49
|
+
if duration>0:d=duration;break
|
|
50
|
+
return round(d/_g10)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from microbit import i2c,pin1,pin2,pin8,pin12,pin13,pin14,pin15,sleep
|
|
2
|
+
import gc,machine,music,neopixel
|
|
3
|
+
_g1=50
|
|
4
|
+
_g2=40
|
|
5
|
+
_g3=40
|
|
6
|
+
_g4=bytearray(5)
|
|
7
|
+
_g5=bytearray(2)
|
|
8
|
+
_g6=bytes(b'\x00\x0b\x0b\x0c\x0c\r\r\r\x0e\x0e\x0f\x0f\x0f\x10\x10\x11\x11\x11\x12\x12\x13\x13\x13\x14\x14\x15\x15\x15\x16\x16\x17\x17\x17\x18\x19\x1a\x1b\x1b\x1c\x1d\x1e\x1f !"##$%&\'()*++,-./0123468:<?ADFILOSVZ^bgkpu{\x81\x87\x8d\x94\x9b\xa3\xab\xb4\xbd\xc6\xd0\xdb\xe6\xf2\xff')
|
|
9
|
+
_g7=0
|
|
10
|
+
_g8=0
|
|
11
|
+
_g9=0
|
|
12
|
+
_g10=neopixel.NeoPixel(pin15,4)
|
|
13
|
+
np_rgb_pixels=_g10
|
|
14
|
+
_g11=['c5:1','r','c5:1','r:3']
|
|
15
|
+
_g12='Please connect to Maqueen robot and switch it on.'
|
|
16
|
+
def _f1(dirL,powerL,dirR,powerR):
|
|
17
|
+
global _g4;_g4[1]=dirL;_g4[2]=powerL;_g4[3]=dirR;_g4[4]=powerR
|
|
18
|
+
try:i2c.write(16,_g4)
|
|
19
|
+
except:raise RuntimeError(_g12)
|
|
20
|
+
def _f2(side,dir,power):
|
|
21
|
+
global _g4;_g4[1+side]=dir;_g4[2+side]=power
|
|
22
|
+
try:i2c.write(16,_g4)
|
|
23
|
+
except:raise RuntimeError(_g12)
|
|
24
|
+
def _f3(speed,offset):return min(_g6[speed]+offset,255)
|
|
25
|
+
def _f4(r):
|
|
26
|
+
A=_g1;B=int(r*100);D=A-max(B+20,40)
|
|
27
|
+
if D<=0:A=min(max(B+40,40),100)
|
|
28
|
+
C=0
|
|
29
|
+
if B>=4:E=(100-A)//2;C=(B*10-35)/(B*(11+(_g9-4)/10)+90+E);C=C*A
|
|
30
|
+
F=_f3(int(C),0);G=_f3(int(A),0);return F,G
|
|
31
|
+
def calibrate(offset,differential=0,arcScaling=0):global _g8;global _g7;global _g9;_g7=max(min(int(offset),50),-10);_g8=max(min(int(differential),150),-150);_g9=max(min(arcScaling,50),-15);setSpeed(_g1)
|
|
32
|
+
def setSpeed(speed):
|
|
33
|
+
global _g1;global _g2;global _g3;_g1=int(min(max(speed,0),100));A=_f3(_g1,_g7);B=round((1-_g1/100)*abs(_g8))if _g1>0 else 0;C=round(_g1/100*abs(_g8))
|
|
34
|
+
if _g8>0:_g2=A-C;_g3=A+B
|
|
35
|
+
else:_g2=A+B;_g3=A-C
|
|
36
|
+
def resetSpeed():setSpeed(50)
|
|
37
|
+
def stop():_f1(0,0,0,0)
|
|
38
|
+
def forward():_f1(0,_g2,0,_g3)
|
|
39
|
+
def backward():_f1(1,_g2,1,_g3)
|
|
40
|
+
def left():_f1(1,_g2,0,_g3)
|
|
41
|
+
def right():_f1(0,_g2,1,_g3)
|
|
42
|
+
def rightArc(radius):A,B=_f4(radius);_f1(0,B,0,A)
|
|
43
|
+
def leftArc(radius):A,B=_f4(radius);_f1(0,A,0,B)
|
|
44
|
+
class Motor:
|
|
45
|
+
def __init__(A,side):A._side=side
|
|
46
|
+
def rotate(B,speed):A=speed;C=int(min(max(abs(A),0),100));D=_f3(C,_g7);E=0 if A>0 else 1;_f2(B._side,E,D)
|
|
47
|
+
def setServo(servo,angle):
|
|
48
|
+
B=angle;A=servo;global _g5
|
|
49
|
+
if A=='S1'or A=='P0':_g5[0]=20
|
|
50
|
+
elif A=='S2'or A=='P1':_g5[0]=21
|
|
51
|
+
else:raise ValueError("Unknown Servo. Please use 'S1' or 'S2'.")
|
|
52
|
+
if B<0 or B>180:raise ValueError('Invalid angle. Must be between 0 and 180')
|
|
53
|
+
_g5[1]=B
|
|
54
|
+
try:i2c.write(16,_g5)
|
|
55
|
+
except:raise RuntimeError(_g12)
|
|
56
|
+
class IRSensor:
|
|
57
|
+
def __init__(A,pin):A._pin=pin
|
|
58
|
+
def read_digital(A):return A._pin.read_digital()
|
|
59
|
+
def read_analog(A):raise NameError('Maqueen Lite does not support reading analog sensor values.')
|
|
60
|
+
def getDistance():pin1.write_digital(1);pin1.write_digital(0);A=machine.time_pulse_us(pin2,1,50000);B=(A>>6)+(A>>10)+(A>>11)+(A>>12)+1;return max(min(B,500),0)if B>0 else 255
|
|
61
|
+
def setLED(state,stateR=None):B=state;A=stateR;A=A if A!=None else B;pin8.write_digital(B);pin12.write_digital(A)
|
|
62
|
+
def setLEDLeft(state):pin8.write_digital(state)
|
|
63
|
+
def setLEDRight(state):pin12.write_digital(state)
|
|
64
|
+
def fillRGB(red,green,blue):
|
|
65
|
+
for A in range(4):_g10[A]=red,green,blue
|
|
66
|
+
_g10.show()
|
|
67
|
+
setRGB=fillRGB
|
|
68
|
+
def clearRGB():_g10.clear()
|
|
69
|
+
def posRGB(position,red,green,blue):
|
|
70
|
+
A=position
|
|
71
|
+
if A<0 or A>3:raise ValueError('invalid RGB-LED position. Must be 0,1,2 or 3.')
|
|
72
|
+
_g10[A]=red,green,blue;_g10.show()
|
|
73
|
+
def setAlarm(state):
|
|
74
|
+
if state:music.play(_g11,wait=False,loop=True)
|
|
75
|
+
else:music.stop()
|
|
76
|
+
def beep():music.pitch(440,200,wait=False)
|
|
77
|
+
pin2.set_pull(pin2.NO_PULL)
|
|
78
|
+
delay=sleep
|
|
79
|
+
irLeft=IRSensor(pin13)
|
|
80
|
+
irRight=IRSensor(pin14)
|
|
81
|
+
motL=Motor(0)
|
|
82
|
+
motR=Motor(2)
|