forked from dhdjhdj/MiniCar
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path代码文本文档2.txt
236 lines (213 loc) · 6.49 KB
/
代码文本文档2.txt
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
let address = 0x30
enum Motorlist {
//% block="A"
M1 = 1,
//% block="B"
M2 = 2
}
enum Direction1 {
//% block="Forward"
Forward = 1,
//% block="Backward"
Backward = 0
}
enum LED_rgb_L_R {
//% bolck="LED_R"
LED_R = 1,
//% bolck="LED_L"
LED_L = 0,
}
enum LED_color {
//% block="rad"
red1 = 1,
//% block="green"
green1 = 2,
//% block="blue"
blue1 = 3,
//% block="cyan"
cyan = 4,
//% block="oxblood red"
oxbloodred = 5,
//% block="white"
white = 6,
//% block="yellow"
yellow = 7,
//% block="Turn off LED"
black = 8,
}
enum pwm_led_l {
//% black="red"
pwm_red_l = 0x08,
//% black="green"
pwm_green_l = 0x07,
//% black="blue"
pwm_blue_l = 0x06,
}
enum pwm_led_r {
//% black="red"
pem_red_r = 0x09,
//% black="green"
pwm_green_r = 0x0a,
//% black="blue"
pwm_blue_r = 0x05,
}
//% color="#AA278D"
namespace MiniCar {
//% block="motor = | %motor Direction = | $direction speed = $pwmvalue"
//% direction.shadow=timePicker
//% pwmvalue.min=0 pwmvalue.max=255
//% group="Motor" weight=65
export function motor(motor: Motorlist, direction: Direction1, pwmvalue: number) {
switch (motor) {
case 1: // M1电机控制
if (direction) { motor_i2cWrite(0x01, pwmvalue); motor_i2cWrite(0x02, 0); }
else { motor_i2cWrite(0x02, pwmvalue); motor_i2cWrite(0x01, 0); }
break;
case 2: // M2电机控制
if (direction) { motor_i2cWrite(0x04, pwmvalue); motor_i2cWrite(0x03, 0); }
else { motor_i2cWrite(0x03, pwmvalue); motor_i2cWrite(0x04, 0); }
break;
}
}
//% block="LED Show"
//% group="RGB LED" weight=65
export function led_show() {
let a, s, d;
motor_i2cWrite(0x07, 255); motor_i2cWrite(0x06, 255);
motor_i2cWrite(0x0a, 255); motor_i2cWrite(0x05, 255);
//红色逐渐点亮
for (a = 255; a > 1; a--) {
motor_i2cWrite(0x08, a);
motor_i2cWrite(0x09, a);
basic.pause(5);
}
//绿色逐渐点亮
for (s = 255; s > 1; s--) {
motor_i2cWrite(0x07, s);
motor_i2cWrite(0x0a, s);
basic.pause(5);
}
//红色逐渐熄灭
for (a = 0; a < 255; a++) {
motor_i2cWrite(0x08, a);
motor_i2cWrite(0x09, a);
basic.pause(5);
}
//blue
for (d = 255; d > 1; d--) {
motor_i2cWrite(0x06, d);
motor_i2cWrite(0x05, d);
basic.pause(5);
}
//green
for (s = 0; s < 255; s++) {
motor_i2cWrite(0x07, s);
motor_i2cWrite(0x0a, s);
basic.pause(5);
}
//rad
for (a = 255; a > 1; a--) {
motor_i2cWrite(0x08, a);
motor_i2cWrite(0x09, a);
basic.pause(5);
}
for (d = 0; d < 255; d++) {
motor_i2cWrite(0x06, d);
motor_i2cWrite(0x05, d);
basic.pause(5);
}
for (a = 0; a < 255; a++) {
motor_i2cWrite(0x08, a);
motor_i2cWrite(0x09, a);
basic.pause(5);
}
}
//% block="LED_R= |%color PWM= |$value"
//% direction.shadow=timePicker
//% value.min=0 value.max=255
//% group="RGB LED" weight=66
export function PWM_LED_R(color: pwm_led_r, value: number) {
motor_i2cWrite(color, value);
}
//% block="LED_L= |%color PWM= |$value"
//% direction.shadow=timePicker
//% value.min=0 value.max=255
//% group="RGB LED" weight=67
export function PWM_LED_L(color: pwm_led_l, value: number) {
motor_i2cWrite(color, value);
}
//% block="LED OFF"
//% group="RGB LED" weight=64
export function LED_OFF() {
motor_i2cWrite(0x08, 255); motor_i2cWrite(0x07, 255); motor_i2cWrite(0x06, 255);
motor_i2cWrite(0x09, 255); motor_i2cWrite(0x0a, 255); motor_i2cWrite(0x05, 255);
}
/**
* Ultrasonic sensor
*/
const TRIG_PIN = DigitalPin.P14;
const ECHO_PIN = DigitalPin.P15;
pins.setPull(TRIG_PIN, PinPullMode.PullNone);
let lastTime = 0;
//% block="Ultrasonic"
//% group="Ultrasonic Sensor" weight=67
export function ultra(): number {
//send trig pulse
pins.digitalWritePin(TRIG_PIN, 0)
control.waitMicros(2);
pins.digitalWritePin(TRIG_PIN, 1)
control.waitMicros(10);
pins.digitalWritePin(TRIG_PIN, 0)
// read echo pulse max distance : 6m(35000us)
//2020-7-6
// pins.pulseIn():This function has a bug and returns data with large errors.
let t = pins.pulseIn(ECHO_PIN, PulseValue.High, 35000);
let ret = t;
//Eliminate the occasional bad data
if (ret == 0 && lastTime != 0) {
ret = lastTime;
}
lastTime = t;
//2020-7-6
//It would normally divide by 58, because the pins.pulseIn() function has an error, so it's divided by 58
return Math.round(ret / 58);
}
/**
* photoresistance sensor
*/
//% block="LDR_L "
//% group="Photoresistance Sensor" weight=66
export function PH1(): number {
return pins.analogReadPin(AnalogPin.P1);
}
//% block="LDR_R "
//% group="Photoresistance Sensor" weight=66
export function PH2(): number {
return pins.analogReadPin(AnalogPin.P0);
}
/**
* return 0b01 or 0b10
* 0b01 is the sensor on the left
* 0b10 is the sensor on the right
*/
pins.setPull(DigitalPin.P12, PinPullMode.PullNone);
pins.setPull(DigitalPin.P13, PinPullMode.PullNone);
//% block="Line Tracking"
//% group="Line Tracking" weight=68
export function LineTracking(): number {
let val = pins.digitalReadPin(DigitalPin.P12) << 0 | pins.digitalReadPin(DigitalPin.P13) << 1;
return val;
}
//% block="set servo to angle %angle"
//% group="Servo" weight=69
//% angle.min=0 angle.max.max=180
export function setServo(angle: number): void {
pins.servoWritePin(AnalogPin.P2, angle)
}
}
function motor_i2cWrite(reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(address, buf)
}