-
Notifications
You must be signed in to change notification settings - Fork 15
/
clawbot.js
101 lines (93 loc) · 2.15 KB
/
clawbot.js
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
var powerMotorLeft = brick.motor(M2);
var powerMotorRight = brick.motor(M1);
var motorHandUpDown = brick.motor(M4);
var handServo = brick.motor(M3);
var lightL = brick.sensor(A1);
var lightR = brick.sensor(A2);
var distance = brick.sensor(D1);
var stopBack = false;
var stopForward = false;
var Gy = 0;
gamepad.pad.connect(
function(padId, x, y) {
if (padId == 1) {
motorHandUpDown.setPower(-y)/10;
handServo.setPower(x)/10;
}
if (padId == 2) {
Gy = y;
if(stopBack) {
Gy = Gy > 0 ? Gy : 0;
}
if(stopForward) {
Gy = Gy < 0 ? Gy : 0;
}
powerMotorLeft.setPower(Gy+x);
powerMotorRight.setPower(Gy-x);
}
}
)
gamepad.padUp.connect(
function(padId, x, y) {
if (padId == 1) {
motorHandUpDown.powerOff();
handServo.powerOff();
} else if (padId == 2) {
powerMotorLeft.powerOff();
powerMotorRight.powerOff();
}
}
)
gamepad.button.connect(
function(buttonId, pressed) {
switch (buttonId) {
case 1: {
brick.say("Hello, i am TRIK");
break;
}
case 2: {
brick.sadSmile();
script.wait(2000);
brick.smile();
break;
}
case 5: {
script.system("/etc/init.d/mjpg-encoder-ov7670 restart");
script.wait(3000);
script.system("/etc/init.d/mjpg-streamer-ov7670.sh restart");
}
default: break;
}
}
)
var dold = 0;
var bordersLoop = function() {
if(lightL.read() > 6 || lightR.read() > 6) {
stopBack = true;
if(Gy < 0) {
powerMotorLeft.powerOff();
powerMotorRight.powerOff();
}
} else {
stopBack = false;
}
var d = distance.read()
if((d + dold) / 2 < 30) {
stopForward = true;
if(Gy > 0) {
powerMotorLeft.powerOff();
powerMotorRight.powerOff();
}
} else {
stopForward = false;
}
print(lightL.read(), lightR.read(), (d + dold) / 2);
dold = d;
}
var timer = script.timer(100);
timer.timeout.connect(bordersLoop);
timer.start();
script.system("/etc/init.d/mjpg-encoder-ov7670 restart");
script.wait(3000);
script.system("/etc/init.d/mjpg-streamer-ov7670.sh restart");
script.run();