-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.ts
More file actions
80 lines (66 loc) · 1.94 KB
/
Copy pathmain.ts
File metadata and controls
80 lines (66 loc) · 1.94 KB
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
/**
* Control keyestudio servoCar
* author: jieliang mo
* github:https://github.com/mworkfun
* Write the date: 2020-5-27
*/
//% color="#ff6800" icon="\uf1b9"
//% groups='["Car", "Servo", "Configuration"]'
namespace ServoCar {
//% fixedInstances
export class Servo {
private _minAngle: number;
private _maxAngle: number;
constructor() {
this._minAngle = 0;
this._maxAngle = 180;
}
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 {
}
/**
* 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);
}
InternalSetAngle(angle: number): void {
this._pin.servoWrite(angle);
}
}
}