-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxt-pendulum-str-ct.c
66 lines (58 loc) · 1.77 KB
/
nxt-pendulum-str-ct.c
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
struct diff_val{
float val;
float lval;
};
#define dv diff_val
void go(int speed);
void set_v(dv tmp,float val);
float dif(dv tmp);
//+----+-------+-------+------+-----------------+
//| ## | const | value | unit | desc. |
//+----+-------+-------+------+-----------------+
#define L 0 // cm | pendulum length |
//+----+-------+-------+------+-----------------+
#define k1 0 // any | coefficient |
//+----+-------+-------+------+-----------------+
#define k2 0 // any | coefficient |
//+----+-------+-------+------+-----------------+
#define k3 0 // any | coefficient |
//+----+-------+-------+------+-----------------+
#define delay 30 // ms | cycle delay |
//+----+-------+-------+------+-----------------+
int dt = 0;
task main(){
SetSensorLowspeed(S1);
ResetSensorHTAngle(S1, HTANGLE_MODE_CALIBRATE);
float rpm,abs_angle,alg_angle,avg_mrc;
// k2*` îò MRC + k3*` îò Sen + k1*Sen
dv motor_rot_cnt;
dv sensor;
motor_rot_cnt.val = sensor.val = 0;
float Fx = 0;
int st = CurrentTick();
while(1){
dt = CurrentTick() - st;
avg_mrc =
(MotorRotationCount(OUT_A) + MotorRotationCount(OUT_B)) / 2;
set_v(motor_rot_cnt,avg_mrc);
ResetSensorHTAngle(S1, HTANGLE_MODE_CALIBRATE);
ReadSensorHTAngle(S1, abs_angle,alg_angle,rpm);
set_v(sensor,abs_angle);
Fx = k1 * sensor.val +
k2 * dif(motor_rot_cnt) +
k3 * dif(sensor);
go(Fx);
Wait(delay);
}
}
float dif(dv tmp){
return (tmp.val - tmp.lval) / delay;
}
void set_v(dv tmp,float val){
tmp.lval = tmp.val;
tmp.val = val;
}
void go(int speed){
OnFwd(OUT_A,speed);
OnFwd(OUT_B,-speed);
}