2018-05-06 08:14:51 +02:00
|
|
|
#include <math.h>
|
2018-05-06 18:35:26 +02:00
|
|
|
#include <pthread.h>
|
2018-02-16 15:44:45 +01:00
|
|
|
#include <stdlib.h>
|
2018-05-06 18:35:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-02-16 15:44:45 +01:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
#include "debug.h"
|
2018-05-06 08:14:51 +02:00
|
|
|
#include "motor.h"
|
|
|
|
#include "movement.h"
|
2018-05-01 08:45:02 +02:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
pthread_t tMovement;
|
|
|
|
struct position cons;
|
|
|
|
pthread_mutex_t movCons;
|
|
|
|
pthread_mutex_t movEnableMutex;
|
|
|
|
pthread_cond_t movEnableCond;
|
|
|
|
bool movEnableBool;
|
|
|
|
|
|
|
|
float xDiff;
|
|
|
|
float yDiff;
|
|
|
|
float oEcart;
|
|
|
|
float dDirEcart;
|
|
|
|
float oDirEcart;
|
|
|
|
float dErr;
|
|
|
|
float oErr;
|
|
|
|
bool oRetenu;
|
|
|
|
bool dRetenu;
|
|
|
|
float lErr;
|
|
|
|
float rErr;
|
|
|
|
unsigned int nbCalcCons;
|
|
|
|
|
2018-04-04 16:17:13 +02:00
|
|
|
void configureMovement()
|
2018-02-16 15:44:45 +01:00
|
|
|
{
|
2018-05-01 08:45:02 +02:00
|
|
|
stop();
|
2018-05-06 18:35:26 +02:00
|
|
|
|
|
|
|
nbCalcCons = 0;
|
|
|
|
pthread_mutex_init(&movCons, NULL);
|
|
|
|
|
|
|
|
pthread_mutex_init(&movEnableMutex, NULL);
|
|
|
|
pthread_cond_init(&movEnableCond, NULL);
|
|
|
|
movEnableBool = false;
|
|
|
|
|
|
|
|
pthread_create(&tMovement, NULL, TaskMovement, NULL);
|
|
|
|
|
|
|
|
registerDebugVar("xCons", f, &cons.x);
|
|
|
|
registerDebugVar("yCons", f, &cons.y);
|
|
|
|
registerDebugVar("oCons", f, &cons.o);
|
|
|
|
registerDebugVar("xDiff", f, &xDiff);
|
|
|
|
registerDebugVar("yDiff", f, &yDiff);
|
|
|
|
registerDebugVar("oEcart", f, &oEcart);
|
|
|
|
registerDebugVar("dErr", f, &dErr);
|
|
|
|
registerDebugVar("oErr", f, &oErr);
|
|
|
|
registerDebugVar("dDirEcart", f, &dDirEcart);
|
|
|
|
registerDebugVar("oDirEcart", f, &oDirEcart);
|
|
|
|
registerDebugVar("dRetenu", d, &dRetenu);
|
|
|
|
registerDebugVar("oRetenu", d, &oRetenu);
|
|
|
|
registerDebugVar("lErr", f, &lErr);
|
|
|
|
registerDebugVar("rErr", f, &rErr);
|
|
|
|
registerDebugVar("nbCalcCons", d, &nbCalcCons);
|
|
|
|
|
|
|
|
disableConsigne();
|
2018-02-16 15:44:45 +01:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
void setDestination(struct position* pos)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&movCons);
|
|
|
|
memcpy(&cons, pos, sizeof(struct position));
|
|
|
|
pthread_mutex_unlock(&movCons);
|
|
|
|
}
|
|
|
|
|
|
|
|
float angleGap(float target, float actual)
|
|
|
|
{
|
|
|
|
return fmod(target - actual + M_PI, 2 * M_PI) - M_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* TaskMovement(void* pData)
|
|
|
|
{
|
|
|
|
(void)pData;
|
|
|
|
|
|
|
|
unsigned int lastPosCalc = 0;
|
|
|
|
struct position connu;
|
|
|
|
|
|
|
|
oRetenu = true;
|
|
|
|
dRetenu = true;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
// Test if enabled
|
|
|
|
pthread_mutex_lock(&movEnableMutex);
|
|
|
|
while (!movEnableBool) {
|
|
|
|
pthread_cond_wait(&movEnableCond, &movEnableMutex);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&movEnableMutex);
|
|
|
|
|
|
|
|
lastPosCalc = getPositionNewer(&connu, lastPosCalc);
|
|
|
|
// Destination → ordre
|
|
|
|
pthread_mutex_lock(&movCons);
|
|
|
|
xDiff = cons.x - connu.x;
|
|
|
|
yDiff = cons.y - connu.y;
|
|
|
|
oEcart = angleGap(cons.o, connu.o);
|
|
|
|
|
|
|
|
dDirEcart = hypotf(xDiff, yDiff);
|
|
|
|
oDirEcart = angleGap(atan2(yDiff, xDiff), connu.o);
|
|
|
|
pthread_mutex_unlock(&movCons);
|
|
|
|
|
|
|
|
if (dDirEcart > D_DIR_ECART_MAX) {
|
|
|
|
oRetenu = true;
|
|
|
|
} else if (dDirEcart < D_DIR_ECART_MIN) {
|
|
|
|
oRetenu = false;
|
|
|
|
}
|
|
|
|
oErr = oRetenu ? oDirEcart : oEcart;
|
|
|
|
|
|
|
|
float oDirEcartAbs = fabs(oDirEcart);
|
|
|
|
if (oDirEcartAbs > O_DIR_ECART_MAX) {
|
|
|
|
dRetenu = true;
|
|
|
|
} else if (oDirEcartAbs < O_DIR_ECART_MIN) {
|
|
|
|
dRetenu = false;
|
|
|
|
}
|
|
|
|
dErr = dRetenu ? 0 : dDirEcart;
|
|
|
|
|
|
|
|
// Ordre → Volt
|
|
|
|
float dErrRev = dErr / WHEEL_PERIMETER;
|
|
|
|
float oErrRev = O_GAIN * oErr * DISTANCE_BETWEEN_WHEELS / WHEEL_PERIMETER;
|
|
|
|
|
|
|
|
lErr = dErrRev - oErrRev;
|
|
|
|
rErr = dErrRev + oErrRev;
|
|
|
|
|
|
|
|
// PID
|
|
|
|
float lVoltCons = P * lErr;
|
|
|
|
float rVoltCons = P * rErr;
|
|
|
|
|
|
|
|
setMoteurTension(lVoltCons, rVoltCons);
|
|
|
|
|
|
|
|
nbCalcCons++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-04 16:17:13 +02:00
|
|
|
|
2018-05-01 09:03:33 +02:00
|
|
|
void deconfigureMovement()
|
|
|
|
{
|
|
|
|
stop();
|
2018-05-06 18:35:26 +02:00
|
|
|
pthread_cancel(tMovement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void enableConsigne()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&movEnableMutex);
|
|
|
|
movEnableBool = true;
|
|
|
|
pthread_cond_signal(&movEnableCond);
|
|
|
|
pthread_mutex_unlock(&movEnableMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disableConsigne()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&movEnableMutex);
|
|
|
|
movEnableBool = false;
|
|
|
|
// No signal here, will be disabled on next run
|
|
|
|
pthread_mutex_unlock(&movEnableMutex);
|
2018-05-01 09:03:33 +02:00
|
|
|
}
|