forked from Jaycode1996/ServoCar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
272 lines (242 loc) · 7.1 KB
/
main.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Control keyestudio DeskBitCar
* author: xiuxian
* github:https://github.com/Ungravitation/DeskBit
* Write the date: 2021-01-05
*/
enum state {
state1=0x10,
state2=0x11,
state3=0x20,
state4=0x21
}
interface KV {
key: state;
action: Action;
}
//% color="#ff6800" icon="\uf1b9"
//% groups='["Car", "Servo", "Configuration"]'
namespace DeskBit {
//% fixedInstances
export class Servo {
private _minAngle: number;
private _maxAngle: number;
private _stopOnNeutral: boolean;
constructor() {
this._minAngle = 0;
this._maxAngle = 180;
this._stopOnNeutral = false;
}
private clampDegrees(degrees: number): number {
degrees = degrees | 0;
degrees = Math.clamp(this._minAngle, this._maxAngle, degrees);
return degrees;
}
/**
* Set the servo angle
*/
//% weight=100 help=servos/set-angle
//% blockId=servoservosetangle block="set %servo angle to %degrees=protractorPicker °"
//% degrees.defl=90
//% servo.fieldEditor="gridpicker"
//% servo.fieldOptions.width=220
//% servo.fieldOptions.columns=2
//% parts=microservo trackArgs=0
//% group="Servo"
setAngle(degrees: number) {
degrees = this.clampDegrees(degrees);
this.internalSetAngle(degrees);
}
protected internalSetAngle(angle: number): void {
}
/**
* Stop sending commands to the servo so that its rotation will stop at the current position.
*/
// On a normal servo this will stop the servo where it is, rather than return it to neutral position.
// It will also not provide any holding force.
//% weight=10 help=servos/stop
//% blockId=servoservostop block="stop %servo"
//% servo.fieldEditor="gridpicker"
//% servo.fieldOptions.width=220
//% servo.fieldOptions.columns=2
//% parts=microservo trackArgs=0
//% group="Servo"
stop() {
this.internalStop();
}
protected internalStop() { }
/**
* Gets the minimum angle for the servo
*/
public get minAngle() {
return this._minAngle;
}
/**
* Gets the maximum angle for the servo
*/
public get maxAngle() {
return this._maxAngle;
}
}
export class PinServo extends Servo {
private _pin: PwmOnlyPin;
constructor(pin: PwmOnlyPin) {
super();
this._pin = pin;
}
protected internalSetAngle(angle: number): void {
this._pin.servoWrite(angle);
}
protected internalSetPulse(micros: number): void {
this._pin.servoSetPulse(micros);
}
protected internalStop() {
this._pin.digitalWrite(false);
}
InternalSetAngle(angle: number): void {
this._pin.servoWrite(angle);
}
}
let leftWheel = new PinServo(pins.P2);
let rightWheel = new PinServo(pins.P1);
let shovel = new PinServo(pins.P0);
/**
* Set the car speed
* angle can control speed
*/
//% block="Car %directe speed: %Speed"
//% Speed.min=0 Speed.max=100
//% group="Car"
export function Run(directe: DIR, Speed: number) {
let CW = Math.map(Speed, 0, 100, 90, 180);
let CCW = Math.map(Speed, 0, 100, 90, 0);
if (directe == 0) {
leftWheel.InternalSetAngle(CW);
rightWheel.InternalSetAngle(CCW);
}
if (directe == 1) {
leftWheel.InternalSetAngle(CCW);
rightWheel.InternalSetAngle(CW);
}
if (directe == 2) {
leftWheel.InternalSetAngle(CW);
rightWheel.InternalSetAngle(CW);
}
if (directe == 3) {
leftWheel.InternalSetAngle(CCW);
rightWheel.InternalSetAngle(CCW);
}
}
/**
* Set the car speed
* angle can control speed
*/
//% block="Car %select wheel %directe speed: %Speed"
//% Speed.min=0 Speed.max=100
//% group="Car"
export function Run2(select: L_R, directe: F_B, Speed: number) {
let CW = Math.map(Speed, 0, 100, 90, 180);
let CCW = Math.map(Speed, 0, 100, 90, 0);
if ((select == 0) && (directe == 0)) {
leftWheel.InternalSetAngle(CW);
}
if ((select == 0) && (directe == 1)) {
leftWheel.InternalSetAngle(CCW);
}
if ((select == 1) && (directe == 0)) {
rightWheel.InternalSetAngle(CCW);
}
if ((select == 1) && (directe == 1)) {
rightWheel.InternalSetAngle(CW);
}
}
/**
* Car stop
*/
//% block="Car Stop"
//% group="Car"
export function Stop() {
leftWheel.InternalSetAngle(90);
rightWheel.InternalSetAngle(90);
}
/**
* car's shovel
*/
//% block="Angle of Car's arm: %angle °"
//% angle.min=0 angle.max=180
//% group="Car"
export function Shovel(angle: number) {
shovel.InternalSetAngle(angle);
}
let irstate:number;
let state:number;
//% advanced=true shim=maqueenIRV2::irCode
function irCode(): number {
return 0;
}
//% weight=5
//% group="micro:bit(v2)"
//% blockId=IR_readv2 block="read IR key value"
export function IR_readV2(): number {
return valuotokeyConversion();
}
//% weight=2
//% group="micro:bit(v2)"
//% blockId=IR_callbackUserv2 block="on IR received"
//% draggableParameters
export function IR_callbackUserV2(cb: (message: number) => void) {
state = 1;
control.onEvent(11, 22, function() {
cb(irstate)
})
}
function valuotokeyConversion():number{
let irdata:number;
switch(irCode()){
case 0xb946:irdata = 12;break;
case 0xbb44:irdata = 13;break;
case 0xea15:irdata = 14;break;
case 0xbc43:irdata = 15;break;
case 0xbf40:irdata = 16;break;
case 0xad52:irdata = 0;break;
case 0xe916:irdata = 1;break;
case 0xe619:irdata = 2;break;
case 0xf20d:irdata = 3;break;
case 0xf30C:irdata = 4;break;
case 0xe718:irdata = 5;break;
case 0xa15e:irdata = 6;break;
case 0xf708:irdata = 7;break;
case 0xe31c:irdata = 8;break;
case 0xa55a:irdata = 9;break;
case 0xbd42:irdata = 10;break;
case 0xb54a:irdata = 11;break;
default:
irdata = -1;
}
return irdata;
//return irCode();
}
basic.forever(() => {
if(state == 1){
irstate = valuotokeyConversion();
if(irstate != -1){
control.raiseEvent(11, 22)
}
}
basic.pause(20);
})
}
enum DIR {
runForward = 0,
runBack = 1,
leftRotation = 3,
rightRotation = 2,
}
enum L_R {
left = 0,
right = 1,
}
enum F_B {
forward = 0,
back = 1,
}