-
Notifications
You must be signed in to change notification settings - Fork 15
/
cockroach-fischertechnik.js
63 lines (49 loc) · 1.36 KB
/
cockroach-fischertechnik.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
/* Copyright 2013 Nikita Batov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
//Script example for TRIK-based models
var s1 = 0 // first sensor
var s2 = 0 // second sensor
var u = 0 // delta
var s1old = 0 // old value of s1
var s2old = 0 // old value of s2
var i = 0
const v = 85 // start speed
const k1 = 2.3 // magic numbers
const k2 = 20
function stab()
{
for(;;) //Infinity loop. Script is running = Robot is moving
{
i++;
//Get sensors data
s1 = brick.analogSensor("1").read()
s2 = brick.analogSensor("2").read()
//Delta
u = (-1)*k1*(s2-s1) + k2*((s1 - s1old)-(s2-s2old))
//Debug print
if (i == 10)
{
print("power 1 = ", v+u)
print("power 2 = ", v-u)
i=0;
}
//Setting power on motors
brick.powerMotor(1).setPower(v+u)
brick.powerMotor(3).setPower(v-u)
//Save old values
s2old=s2
s1old=s1
}
}
stab();