-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
74 lines (72 loc) · 2.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
function Left (speed: number) {
if (speed > 0) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, speed)
} else if (speed < 0) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CCW, 0 - speed)
} else {
maqueen.motorStop(maqueen.Motors.M1)
}
}
input.onButtonPressed(Button.A, function () {
serial.writeLine("--")
serial.writeValue("A", 1)
serial.writeValue("y", y)
serial.writeValue("x", x)
serial.writeValue("sLeft", Math.max(-255, Math.min((y + x) / 2, 255)))
serial.writeValue("sRight", Math.max(-255, Math.min((y - x) / 2, 255)))
})
radio.onReceivedValue(function (name, value) {
if (name == "x") {
if (value >= x0) {
x = Math.map(value, x0, 1023, 0, 512)
} else {
x = Math.map(value, 0, x0, -512, 0)
}
} else if (name == "y") {
if (value >= y0) {
y = Math.map(value, y0, 1023, 0, 512)
} else {
y = Math.map(value, 0, y0, -512, 0)
}
} else if (name == "Dial") {
Dial = Math.map(value, 5, 800, 0, 255)
} else if (name == "Red") {
if (value == 1) {
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, Dial)
} else {
maqueen.motorStop(maqueen.Motors.All)
}
} else if (name == "cal_x") {
x0 = value
serial.writeValue(name, value)
} else if (name == "cal_y") {
y0 = value
serial.writeValue(name, value)
} else if (false) {
} else {
}
})
function Right (speed: number) {
if (speed > 0) {
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, speed)
} else if (speed < 0) {
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CCW, 0 - speed)
} else {
maqueen.motorStop(maqueen.Motors.M2)
}
}
let Dial = 0
let x = 0
let y = 0
let y0 = 0
let x0 = 0
x0 = 582
y0 = 568
radio.setGroup(1)
basic.showIcon(IconNames.Happy)
serial.writeLine("Started")
serial.setBaudRate(BaudRate.BaudRate115200)
basic.forever(function () {
Left(Math.max(-255, Math.min((y + x) / 2, 255)))
Right(Math.max(-255, Math.min((y - x) / 2, 255)))
})