-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitFlapv11.ino
381 lines (336 loc) · 13.4 KB
/
splitFlapv11.ino
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AnimationStepper.h> //library perso
Adafruit_MotorShield AFMS1(0x60); // Default address, no jumpers 00000
Adafruit_MotorShield AFMS2(0x61); // 00001
Adafruit_MotorShield AFMS3(0x62); // 00010
Adafruit_MotorShield AFMS4(0x63); // 00011
//variables statiques
const int nombreDEtape = 17; //nombre d'étape dans une animation (commun a toutes les animations)
const int nombreAnimation = 7; //il y a 3 animation dans la classe resources en ce moment.
const bool boolRelease = false; //fonction release;
const bool debug = false;
const int debugAnim = 7;
//const int box = 1; //numéro de la boite :)
const int offSetZero = 0; //offset général à la suite de la calibration
//const int boxOffset[3][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; //offset pécis
const bool calibrationOnly = true;
const int nombreDeMoteur = 8;
const int capteur[8] = {3, 10, 9, 6, 5, 11, A0, A1};
//05,01,02,03,04,00,I0,I1
//c1,c2,c3,c4,c5,c6,c7,c8
//racourcir de :36, 0,16, 0, 0,28, 18, 0;
const int calibFinalPosition[8] = {48, 40, 32, 192, 24, 16, 8, 0} ;
const int boundaryTiming[2] = {25,40};
const int delayAlafinDeLaCalibration = 2; //en secondes
const int frequenceCalibration = 10; //calibration toute les 20 animations
const int vitesseCalibration = 100; //vitesse calibration.
const int accelerationCalibration = 1000;
const int vitesseAnimation = 100;
float acceleration = 1000.0;
const int temporaire = 0;
//variables
int pointeur = 0; //numero du moteur en calibration
int numeroAnimation = 99; //animation en cour
int curseur = 0; //barre ou curseur d'avance dans l'animation courante
int delayingTime = 0;
int absPosStepper[8] = {0, 0, 0, 0, 0, 0, 0, 0};//position absolu des steppers par rapport au zero défini dans la phase de calibration
bool calibrationBool = true; //vrai pendant la calibration
int decompteMoteur = 0; //valeur incrémentale, quand elle est égale au nombre de moteur, cela veut dire qu'ils ont tous fini leur animation
unsigned long actualTime = 0; //utilitaire pour delay sans "delay()"
unsigned long targetTime = 1; //utilitaire pour delay sans "delay()"
bool waitBool = false; //utilitaire pour delay sans "delay()"
bool capteurState[8] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW}; //états des capteurs 0 = Passage de l'aimant | 1 = rien
int lastAnimation[3] = {99,99,99} ; //animation pécedente
int animationCounter = 0; //compteur d'animation
int positionPrecedente = 0;
bool oneTime = true;
bool addoffset = false;
bool arretSurZero = false;
bool premiereCaptation = true;
//int multipleRandom[20] = {99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};
bool oneTimeDelay = true;
unsigned long targetTimeAnim = 0;
bool oneTimeDelayAnim = true;
bool oneTimeCalib = true;
unsigned long delayCalib = 0;
Adafruit_StepperMotor *stepperContainer[8] = {
AFMS1.getStepper(200, 2)/*1*/,
AFMS2.getStepper(200, 2)/*2*/,
AFMS3.getStepper(200, 2)/*3*/,
AFMS4.getStepper(200, 2)/*4*/,
AFMS1.getStepper(200, 1)/*5*/,
AFMS2.getStepper(200, 1)/*6*/,
AFMS3.getStepper(200, 1)/*7*/,
AFMS4.getStepper(200, 1)/*8*/,
};
AccelStepper temp1(forwardstep1, backwardstep1);
AccelStepper temp2(forwardstep2, backwardstep2);
AccelStepper temp3(forwardstep3, backwardstep3);
AccelStepper temp4(forwardstep4, backwardstep4);
AccelStepper temp5(forwardstep5, backwardstep5);
AccelStepper temp6(forwardstep6, backwardstep6);
AccelStepper temp7(forwardstep7, backwardstep7);
AccelStepper temp8(forwardstep8, backwardstep8);
AccelStepper aStepper[nombreDeMoteur] = {temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8};
//constructeur classe externe
AnimationStepper anim;
void setup() {
//Serial
Serial.begin(9600);
//démarage des Shields
AFMS1.begin();
AFMS2.begin();
AFMS3.begin();
AFMS4.begin();
setGeneralValues(vitesseCalibration,accelerationCalibration);
//initialisation des steppeurs en mode rapide.
for (int i = 0; i < nombreDeMoteur; i++) {
pinMode(capteur[i], INPUT);
stepperContainer[i]->quickstepInit();
}
//setup movement
setMovement(99); // animation de calibration pour commencer.
}
void loop() {
if (calibrationBool) {
//update moteur pour calibration
calibration();
} else {
//update des animations en temps normal
updateAnimation();
}
}
void setMovement(int Id) {
if (Id == 99) {
calibrationBool == true;
for (int i = 0; i < nombreDeMoteur; i++) {
aStepper[i].setSpeed(vitesseCalibration);
aStepper[i].setAcceleration(accelerationCalibration);
aStepper[i].move(9999); // plusieurs tours pour être sur d'atteindre le capteur.
}
} else {
numeroAnimation = Id;
for (int i = 0; i < nombreDeMoteur; i++) {
aStepper[i].setSpeed(vitesseAnimation);
aStepper[i].setAcceleration(acceleration);
aStepper[i].move(deplacement(absPosStepper[i], anim.getValue(numeroAnimation, curseur, i)));
//update absPos
absPosStepper[i] += deplacement(absPosStepper[i], anim.getValue(numeroAnimation, curseur, i));
absPosStepper[i] = absPosStepper[i] % 200;
}
if (anim.getValue(numeroAnimation, curseur, 8) != 0) { // si il y a du delay
delayingTime = anim.getValue(numeroAnimation, curseur, 8);
} else {
delayingTime = 0;
}
}
}
void forwardstep1() { stepperContainer[0]->quickstep(FORWARD);}
void backwardstep1() { stepperContainer[0]->quickstep(BACKWARD);}
void forwardstep2() { stepperContainer[1]->quickstep(FORWARD);}
void backwardstep2() { stepperContainer[1]->quickstep(BACKWARD);}
void forwardstep3() { stepperContainer[2]->quickstep(FORWARD);}
void backwardstep3() { stepperContainer[2]->quickstep(BACKWARD);}
void forwardstep4() { stepperContainer[3]->quickstep(FORWARD);}
void backwardstep4() { stepperContainer[3]->quickstep(BACKWARD);}
void forwardstep5() { stepperContainer[4]->quickstep(FORWARD);}
void backwardstep5() { stepperContainer[4]->quickstep(BACKWARD);}
void forwardstep6() { stepperContainer[5]->quickstep(FORWARD);}
void backwardstep6() { stepperContainer[5]->quickstep(BACKWARD);}
void forwardstep7() { stepperContainer[6]->quickstep(FORWARD);}
void backwardstep7() { stepperContainer[6]->quickstep(BACKWARD);}
void forwardstep8() { stepperContainer[7]->quickstep(FORWARD);}
void backwardstep8() { stepperContainer[7]->quickstep(BACKWARD);}
int deplacement(int ActualPosition, int Destination) {
int stepToMove = 0;
if (Destination != 9999) { // 9999 est une valeur de destination qui indique qu'il faut rester immobile.
if (Destination < ActualPosition) { //si il faut repasser par la position 0 (origine)
stepToMove = (200 - ActualPosition) + Destination;
}
if (Destination > ActualPosition) { //si il ne FAUT PAS repasser par la postion 0 (origine)
stepToMove = Destination - ActualPosition;
}
}
return stepToMove;
}
void updateAnimation() {
if (decompteMoteur == nombreDeMoteur) {
nextStep();
}
else{
decompteMoteur = 0;
for (int i = 0; i < nombreDeMoteur; i++) {
if (aStepper[i].distanceToGo() == 0 ) {
decompteMoteur++;
}
aStepper[i].run();
}
}
}
void nextStep() {
actualTime = millis();
if (delayingTime == 0 ) {
if (curseur < nombreDEtape - 1) {
curseur++;
setMovement(numeroAnimation);
decompteMoteur = 0;
} else {
if(oneTimeDelayAnim){
targetTimeAnim = millis() + floor(random(boundaryTiming[0] ,boundaryTiming[1] ))*1000; //random delay inter-animation
actualTime = millis();
oneTimeDelayAnim = false;
}
if(actualTime > targetTimeAnim){
oneTimeDelayAnim = true;
curseur = 0;
decompteMoteur = 0;
setMovement(randomAnimation(false));
}
}
}else{
if(oneTimeDelay){
targetTime = millis() + delayingTime;
if(boolRelease){
for (int i = 0; i < nombreDeMoteur; i++) {
stepperContainer[i]->release();
}
}
oneTimeDelay = false;
}
if(actualTime > targetTime ){
delayingTime = 0;
oneTimeDelay = true;
if(boolRelease){
for(int i = 0;i<nombreDeMoteur;i++){
stepperContainer[i]->quickstepInit();
}
}
decompteMoteur = 0;
}
}
}
int randomAnimation(bool Redo){
if(debug){
Serial.println(debugAnim);
return debugAnim;
}
else{
int value = 0;
if(!Redo){
animationCounter++;
}
value = floor(random(0,nombreAnimation));
if (animationCounter % frequenceCalibration == 0) {
value = 99;
for(int i = 0; i<nombreDeMoteur ; i++){
aStepper[i].setCurrentPosition(0);
}
calibrationBool = true;
/////////////
Serial.println(value);
return value;
/////////////
} else {
if(value == lastAnimation[0] || value == lastAnimation[1] || value == lastAnimation[2]){
/////////////////////////
return randomAnimation(true);
//return value;
/////////////////////////
}
else{
lastAnimation[2] = lastAnimation[1];
lastAnimation[1] = lastAnimation[0];
lastAnimation[0] = value;
/////////////
Serial.println(value);
return value;
/////////////
}
}
}
}
void calibration() {
///////////////////////////////////////////////////////////// SAFE EXIT SI JAMAIS UN CAPTEUR MERDE OU UN MOTEUR
if(aStepper[pointeur].currentPosition() > 1000){
aStepper[pointeur].move(0);
aStepper[pointeur].setCurrentPosition(0);
absPosStepper[pointeur] = 0;
pointeur++;
addoffset = false;
premiereCaptation = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
capteurState[pointeur] = digitalRead(capteur[pointeur]);
if (aStepper[pointeur].distanceToGo() == 0 && !premiereCaptation && !addoffset) {
//a fais soit 9999 step, soit 100 step depuis zero et donc bool changing
aStepper[pointeur].move(200);
//arret sur le prochain zero
arretSurZero = true;
}
if (capteurState[pointeur] == 0 ) {
//j'ai besoin de faire un 1/2 tour pour verif
if (premiereCaptation) {
aStepper[pointeur].move(100);
premiereCaptation = false;
}
if (arretSurZero) {
arretSurZero = false;
//si arrivé a destination bouge a la position fini et actualise l'absPosStepper
aStepper[pointeur].move(10+calibFinalPosition[pointeur]);
addoffset = true;
}
}
if (addoffset && aStepper[pointeur].distanceToGo() == 0) {
aStepper[pointeur].move(0);
aStepper[pointeur].setCurrentPosition(0);
absPosStepper[pointeur] = calibFinalPosition[pointeur];
absPosStepper[pointeur] = absPosStepper[pointeur] % 200;
pointeur++;
addoffset = false;
premiereCaptation = true;
}
if (pointeur < nombreDeMoteur) {
aStepper[pointeur].run();
} else {
//s.println("fin calibration");
//calibrationBool = true;
actualTime = millis();
if(oneTimeCalib){
delayCalib = millis()+delayAlafinDeLaCalibration*1000;
actualTime = millis();
oneTimeCalib = false;
}
if(actualTime > delayCalib){
waitBool = false;
calibrationBool = false;
setMovement(randomAnimation(false));
pointeur = 0;
}
}
}
/*
void checkSensorPosition() {
for (int i = 0; i < 8; i++) {
capteurState[i] = digitalRead(capteur[i]);
}
}
void compteurDeStep() {
if (digitalRead(capteur[4]) == LOW && oneTime) {
oneTime = false;
int value = aStepper[4].currentPosition() - positionPrecedente;
//s.println(value);
positionPrecedente = aStepper[4].currentPosition();
}
if (digitalRead(capteur[4]) == HIGH) {
oneTime = true;
}
}*/
void setGeneralValues(float vitesse, float accel) {
for (int i = 0; i < nombreDeMoteur; i++) {
aStepper[i].setMaxSpeed(vitesse);
aStepper[i].setAcceleration(accel);
}
}