forked from piergo98/Ping-pong-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avversario.c
204 lines (151 loc) · 5.94 KB
/
Avversario.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "Avversario.h"
int col = 14; // yellow color
void* adversarytask_x(void* arg)
{
int i, T; // task index
int xd, vd; // desired position and speed
int x, v; // actual position and speed
float u1[DIM_B], y, err[DIM_B], u; // temporary variables
struct state temp; //temporary structure
i = get_task_index(arg);
set_activation(i);
err[NOW] = err[BEFORE] = 0;
u1[NOW] = u1[BEFORE] = 0;
while(!end) {
if (start || player) {
vd = 0;
pthread_mutex_lock(&s8);
temp.position = adversary_x.position;
temp.speed = adversary_x.speed;
pthread_mutex_unlock(&s8);
pthread_mutex_lock(&s12);
if (home){
xd = HALF_X;
}
else {
pthread_mutex_lock(&s3);
xd = buffer[NEXT].x;
pthread_mutex_unlock(&s3);
}
pthread_mutex_unlock(&s12);
get_state(&x, &v, &temp);
//errore di posizione
err[NOW] = xd-x;
//controllo di posizione
u1[NOW] = (KP + Ts * KI) * err[NOW] - KP * err[BEFORE] + u1[BEFORE];
//controllo di velocita'
u = u1[NOW] + KD * (vd - v);
y = motor(u, &adv_x_angle);
pthread_mutex_lock(&s10);
update_adversary_state_x(y, T, &temp);
pthread_mutex_unlock(&s10);
u1[BEFORE] = u1[NOW];
err[BEFORE] = err[NOW];
pthread_mutex_lock(&s8);
adversary_x.position = temp.position;
adversary_x.speed = temp.speed;
pthread_mutex_unlock(&s8);
}
if (deadline_miss(i))
show_dmiss(i);
wait_for_activation(i);
}
}
void* adversarytask_z(void* arg)
{
int i, T, home_tmp; // task index
int zd, vd; // desired position and speed
int x, v; // actual position and speed
float u1[DIM_B], y, err[DIM_B], u; // temporary variables
struct state temp; //temporary structure
i = get_task_index(arg);
set_activation(i);
err[NOW] = err[BEFORE] = 0;
u1[NOW] = u1[BEFORE] = 0;
while(!end) {
if (start || player) {
vd = 0;
pthread_mutex_lock(&s9);
temp.position = adversary_z.position;
temp.speed = adversary_z.speed;
pthread_mutex_unlock(&s9);
pthread_mutex_lock(&s12);
home_tmp = home;
pthread_mutex_unlock(&s12);
if (home_tmp){
zd = P4_Z;
}
else {
pthread_mutex_lock(&s3);
zd = buffer[NEXT].z + D;
pthread_mutex_unlock(&s3);
}
get_state(&x, &v, &temp);
//errore di posizione
err[NOW] = (zd-x);
//controllo di posizione
u1[NOW] = (KP + Ts * KI) * err[NOW] - KP * err[BEFORE] + u1[BEFORE];
//controllo di velocita'
u = u1[NOW] + KD * (vd - v);
y = motor(u, &adv_z_angle);
pthread_mutex_lock(&s10);
update_adversary_state_z(y, T, &temp);
pthread_mutex_unlock(&s10);
u1[BEFORE] = u1[NOW];
err[BEFORE] = err[NOW];
pthread_mutex_lock(&s9);
adversary_z.position = temp.position;
adversary_z.speed = temp.speed;
pthread_mutex_unlock(&s9);
}
if (deadline_miss(i))
show_dmiss(i);
wait_for_activation(i);
}
}
void update_adversary_state_x(float y, int T, struct state *robot_tmp)
{
int delta;
if(!player){
delta = (int)y * R; //converte rotazione del motore in movimento lineare
if (delta > X_MAX ){
robot_tmp->position = X_MAX;
robot_tmp->speed = 0;
}
else if (delta < X_MIN){
robot_tmp->position = X_MIN;
robot_tmp->speed = 0;
}
else{
robot_tmp->speed = (delta - robot_tmp->position)/ Ts; //rapp. incrementale
robot_tmp->position = delta;
}
}
else {
robot_tmp->speed = (mouse_x - robot_tmp->position)/ Ts;
robot_tmp->position = mouse_x;
}
}
void update_adversary_state_z(float y, int T, struct state *robot_tmp)
{
int delta;
if (!player){
delta = y * R; //converte rotazione del motore in movimento cinghia
if (delta > Z_MAX + OFFSET_Z ){
robot_tmp->position = Z_MAX + OFFSET_Z;
robot_tmp->speed = 0;
}
else if (delta < X_MIN + OFFSET_Z){
robot_tmp->position = X_MIN + OFFSET_Z;
robot_tmp->speed = 0;
}
else{
robot_tmp->speed = (delta - robot_tmp->position)/ Ts; //rapp. incrementale
robot_tmp->position = delta;
}
}
else {
robot_tmp->speed = (mouse_y - robot_tmp->position)/ Ts;
robot_tmp->position = mouse_y;
}
}