-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbitcar.ts
209 lines (190 loc) · 6.02 KB
/
bitcar.ts
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
199
200
201
202
203
204
205
206
207
208
209
/*****************************************************************************
* | Description : BitCar extension for micro:bit
* | Developer : CH Makered
* | More Info : http://chmakered.com/
******************************************************************************/
enum GrovePin {
//% block="P0"
P0 = DigitalPin.P0,
//% block="P1"
P1 = DigitalPin.P1,
//% block="P2"
P2 = DigitalPin.P2,
//% block="P8"
P8 = DigitalPin.P8,
//% block="P12"
P12 = DigitalPin.P12,
//% block="P16"
P16 = DigitalPin.P16
}
enum GroveAnalogPin {
//% block="P0"
P0 = AnalogPin.P0,
//% block="P1"
P1 = AnalogPin.P1,
//% block="P2"
P2 = AnalogPin.P2
}
enum DistanceUnit {
//% block="cm"
cm,
//% block="inch"
inch
}
enum trick {
//% block="arise"
getUp,
//% block=" +left"
L_forward = AnalogPin.P14,
//% block=" -right"
R_backward = AnalogPin.P15,
//% block=" +right"
R_forward = AnalogPin.P16,
}
enum IRLineSensor {
//% block="left sensor"
left,
//% block=" right sensor"
right
}
/**
* Provides access to BitCar blocks for micro: bit functionality.
*/
//% color=190 icon="\uf126" block= "BitCar"
//% groups="['BitCar Control', 'Sensors']"
namespace BitCar {
let L_backward = AnalogPin.P13;
let L_forward = AnalogPin.P14;
let R_forward = AnalogPin.P15;
let R_backward = AnalogPin.P16;
/**
* Set the motors' speed of BitCar
*/
//% blockId=move
//% block="BitCar: left motor $left \\%, right motor$right \\%"
//% left.shadow="speedPicker"
//% right.shadow="speedPicker"
//% group="BitCar Control"
//% weight=100
export function move(left: number, right: number) {
if (left >= 0) {
pins.analogWritePin(L_backward, 0);
pins.analogWritePin(L_forward, Math.map(left, 0, 100, 0, 1023));
} else if (left < 0) {
pins.analogWritePin(L_backward, Math.map(Math.abs(left), 0, 100, 0, 1023));
pins.analogWritePin(L_forward, 0);
}
if (right >= 0) {
pins.analogWritePin(R_backward, 0);
pins.analogWritePin(R_forward, Math.map(right, 0, 100, 0, 1023));
} else if (right < 0) {
pins.analogWritePin(R_backward, Math.map(Math.abs(right), 0, 100, 0, 1023));
pins.analogWritePin(R_forward, 0);
}
}
/**
* BitCar stop
*/
//% blockId=stop
//% block="BitCar: stop"
//% group="BitCar Control"
//% weight=99
//% blockGap=40
export function stop() {
pins.analogWritePin(L_backward, 0);
pins.analogWritePin(L_forward, 0);
pins.analogWritePin(R_backward, 0);
pins.analogWritePin(R_forward, 0);
}
/**
* When BitCar is still, make it stand up from the ground and then stop, try to tweak the motor speed and the charge time if it failed to do so
*/
//% blockId=standup_still
//% block="BitCar: stand up with speed $speed \\% charge$charge|(ms)"
//% speed.defl=100
//% speed.min=0 speed.max=100
//% charge.defl=250
//% group="BitCar Control"
//% weight=98
//% blockGap=40
export function standup_still(speed: number, charge: number) {
move(-speed, -speed);
basic.pause(200);
move(speed, speed);
basic.pause(charge);
stop();
}
/**
* Check the state of the IR line sensor, the LED indicator is ON if the line is detected by the corresponding sensor
*/
//% blockId=linesensor
//% block="BitCar: line under $sensor|"
//% group="BitCar Control"
//% weight=97
export function linesensor(sensor: IRLineSensor): boolean {
let result: boolean = false;
if (sensor == IRLineSensor.left) {
if (pins.analogReadPin(AnalogPin.P1) < 500) {
result = true;
}
} else if (sensor == IRLineSensor.right) {
if (pins.analogReadPin(AnalogPin.P2) < 500) {
result = true;
}
}
return result;
}
/**
* Line following at a specified speed.
*/
//% blockId=linefollow
//% block="BitCar: follow line at speed $speed \\%"
//% speed.defl=50
//% speed.min=0 speed.max=100
//% group="BitCar Control"
//% weight=96
export function linefollow(speed: number) {
if (linesensor(IRLineSensor.left) && linesensor(IRLineSensor.right)) {
move(speed, speed);
} else {
if (!(linesensor(IRLineSensor.left)) && linesensor(IRLineSensor.right)) {
move(speed, 0);
if (!(linesensor(IRLineSensor.left)) && !(linesensor(IRLineSensor.right))) {
move(speed, 0);
}
} else {
if (!(linesensor(IRLineSensor.right)) && linesensor(IRLineSensor.left)) {
move(0, speed);
if (!(linesensor(IRLineSensor.left)) && !(linesensor(IRLineSensor.right))) {
move(0, speed);
}
}
}
}
}
/**
* Get the distance from Grove-Ultrasonic Sensor, the measuring range is between 2-350cm
*/
//% blockId=grove_ultrasonic
//% block="Ultrasonic Sensor $groveport|: distance in $Unit"
//% group="Sensors"
//% weight=100
export function grove_ultrasonic(groveport: GrovePin, Unit: DistanceUnit): number {
let duration = 0;
let distance = 0;
let distanceBackup = 0;
let port: number = groveport;
pins.digitalWritePin(<DigitalPin>port, 0);
control.waitMicros(2);
pins.digitalWritePin(<DigitalPin>port, 1);
control.waitMicros(10);
pins.digitalWritePin(<DigitalPin>port, 0);
duration = pins.pulseIn(<DigitalPin>port, PulseValue.High, 50000);
if (Unit == DistanceUnit.cm) distance = duration * 153 / 58 / 100;
else distance = duration * 153 / 148 / 100;
if (distance > 0) distanceBackup = distance;
else distance = distanceBackup;
basic.pause(50);
return distance;
}
}