-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathringbitcar.ts
243 lines (175 loc) · 5.92 KB
/
ringbitcar.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org/blocks/custom
*/
//% weight=0 color=#0fbc11 icon="\uf207" block="ringbit车"
namespace ringbitcar {
export enum TrackingStateType {
//% block="● ●" enumval=0
Tracking_State_0,
//% block="● ◌" enumval=1
Tracking_State_1,
//% block="◌ ●" enumval=2
Tracking_State_2,
//% block="◌ ◌" enumval=3
Tracking_State_3,
}
export enum Distance_Unit {
//% block="mm" enumval=0
Distance_Unit_mm,
//% block="cm" enumval=1
Distance_Unit_cm,
//% block="inch" enumval=2
Distance_Unit_inch,
}
let pin_left_wheel = AnalogPin.P1
let pin_right_wheel = AnalogPin.P2
/**
* TODO: initialization ring:bit car
* @param left describe parameter here, eg: AnalogPin.P1
* @param right describe parameter here, eg: AnalogPin.P2
*/
//% weight=10
//% blockId=ringbitcar_init block="set left wheel at pin %left|right wheel at pin %right"
export function init_wheel(left: AnalogPin, right: AnalogPin): void {
// Add code here
pin_left_wheel = left
pin_right_wheel = right
}
/**
* TODO: full speed move forward
*/
//% weight=9
//% blockId=ringbitcar_forward block="go straight at full speed"
export function forward(): void {
// Add code here
pins.servoSetPulse(pin_left_wheel, 2400)
pins.servoSetPulse(pin_right_wheel, 600)
}
/**
* TODO: full speed move back
*/
//% weight=8
//% blockId=ringbitcar_back block="reverse at full speed"
export function back(): void {
// Add code here
pins.servoSetPulse(pin_left_wheel, 600)
pins.servoSetPulse(pin_right_wheel, 2400)
}
/**
* TODO: full speed turn left
*/
//% weight=7
//% blockId=ringbitcar_left block="turn left at full speed"
export function turnleft(): void {
// Add code here
pins.servoSetPulse(pin_left_wheel, 600)
pins.servoSetPulse(pin_right_wheel, 600)
}
/**
* TODO: full speed turn right
*/
//% weight=6
//% blockId=ringbitcar_right block="turn right at full speed"
export function turnright(): void {
// Add code here
pins.servoSetPulse(pin_left_wheel, 2400)
pins.servoSetPulse(pin_right_wheel, 2400)
}
/**
* TODO: stop
*/
//% weight=5
//% blockId=ringbitcar_brake block="brake"
export function brake(): void {
// Add code here
//pins.servoSetPulse(pin_left_wheel, 1500)
//pins.servoSetPulse(pin_right_wheel, 1500)
pins.digitalWritePin(<number>pin_left_wheel, 0)
pins.digitalWritePin(<number>pin_right_wheel, 0)
}
/**
* TODO: self setting speed
* @param m the m from -100 (min) to 100 (max), eg:0
* @param n the n from -100 (min) to 100 (max), eg:0
*/
//% weight=4
//% blockId=ringbitcar_freestyle block="set left wheel speed %m| right wheel speed %n"
//% m.min=-100 m.max=100
//% n.min=-100 n.max=100
export function freestyle(m: number, n: number): void {
// Add code here
if (m > 0) {
pins.servoSetPulse(pin_left_wheel, 1600 + m * 8)
} else if (m < 0) {
pins.servoSetPulse(pin_left_wheel, 1400 + m * 8)
} else pins.servoSetPulse(pin_left_wheel, 1500)
if (n > 0) {
pins.servoSetPulse(pin_right_wheel, 1400 - n * 8)
} else if (n < 0) {
pins.servoSetPulse(pin_right_wheel, 1600 - n * 8)
} else pins.servoSetPulse(pin_right_wheel, 1500)
}
/**
* TODO: line following
*/
//% weight=10
//% advanced=true
//% blockId=ringbitcar_tracking block="tracking state is %state"
export function tracking(state: TrackingStateType): boolean {
let sensor_pin = AnalogPin.P0
if (pin_left_wheel != AnalogPin.P1 && pin_right_wheel != AnalogPin.P1) {
sensor_pin = AnalogPin.P1
} else if (pin_left_wheel != AnalogPin.P2 && pin_right_wheel != AnalogPin.P2) {
sensor_pin = AnalogPin.P2
}
let i = pins.analogReadPin(sensor_pin)
if (i < 100 && state == 0) {
return true;
} else if (i > 100 && i < 200 && state == 1) {
return true;
} else if (i > 200 && i < 300 && state == 2) {
return true;
} else if (i > 300 && i < 400 && state == 3) {
return true;
} else return false;
}
/**
* TODO: get ultrasonic distance
*/
//% weight=9
//% advanced=true
//% blockId=ringbitcar_sonarbit block="ultrasonic distance in unit %distance_unit"
export function ringbitcar_sonarbit(distance_unit: Distance_Unit): number {
let sensor_pin = AnalogPin.P0
if (pin_left_wheel != AnalogPin.P1 && pin_right_wheel != AnalogPin.P1) {
sensor_pin = AnalogPin.P1
} else if (pin_left_wheel != AnalogPin.P2 && pin_right_wheel != AnalogPin.P2) {
sensor_pin = AnalogPin.P2
}
// send pulse
pins.setPull(<number>sensor_pin, PinPullMode.PullNone)
pins.digitalWritePin(<number>sensor_pin, 0)
control.waitMicros(2)
pins.digitalWritePin(<number>sensor_pin, 1)
control.waitMicros(10)
pins.digitalWritePin(<number>sensor_pin, 0)
// read pulse
let d = pins.pulseIn(<number>sensor_pin, PulseValue.High, 23000) // 8 / 340 =
let distance = d * 10 * 5 / 3 / 58
if (distance > 4000) distance = 0
switch (distance_unit) {
case 0:
return distance //mm
break
case 1:
return distance / 10 //cm
break
case 2:
return distance / 25 //inch
break
default:
return 0
}
}
}