1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-05-17 04:26:00 +02:00
cdf2018-principal/chef/src/position.c

165 lines
3.7 KiB
C
Raw Normal View History

2018-04-04 16:17:13 +02:00
/*
* Fonctions de calcul de la position du robot
*/
2018-05-02 05:51:14 +02:00
#include <math.h>
2018-05-06 18:35:26 +02:00
#include <stdio.h>
2018-05-11 05:34:06 +02:00
#include <stdlib.h>
2018-05-06 18:35:26 +02:00
#include <string.h>
#include <time.h>
2018-05-06 18:35:26 +02:00
#include <unistd.h>
2018-04-04 16:17:13 +02:00
2018-04-29 09:38:49 +02:00
#include "debug.h"
2018-05-02 05:51:14 +02:00
#include "dimensions.h"
2018-05-11 05:34:06 +02:00
#include "fpga.h"
2018-05-06 18:35:26 +02:00
#include "position.h"
2018-05-11 15:58:18 +02:00
#include "common.h"
2018-04-29 09:38:49 +02:00
// Globales
2018-05-02 05:51:14 +02:00
struct position connu;
2018-05-06 18:35:26 +02:00
pthread_mutex_t posConnu;
pthread_cond_t newPos;
2018-04-29 09:38:49 +02:00
pthread_t tPosition;
// Globales
unsigned int nbCalcPos;
long lCodTot, rCodTot;
2018-05-11 15:58:18 +02:00
uint16_t oldL, oldR;
uint16_t newL, newR;
2018-05-11 05:34:06 +02:00
int16_t deltaL, deltaR;
2018-05-11 15:58:18 +02:00
int newLdbg, newRdbg;
struct timespec lastCoderRead;
2018-05-09 14:24:12 +02:00
void updateDelta()
{
2018-05-11 15:58:18 +02:00
newL = (readI2C(fdFPGA(), CODER_LEFT_H) << 8 | readI2C(fdFPGA(), CODER_LEFT_L)) & 0xFFFF;
newR = (readI2C(fdFPGA(), CODER_RIGHT_H) << 8 | readI2C(fdFPGA(), CODER_RIGHT_L)) & 0xFFFF;
newLdbg = newL;
newRdbg = newR;
deltaL = (abs(oldL - newL) < UINT16_MAX / 2) ? newL - oldL : UINT16_MAX - oldL + newL;
deltaR = (abs(oldR - newR) < UINT16_MAX / 2) ? newR - oldR : UINT16_MAX - oldR + newR;
// Verification de valeur abbérante
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
float maxDelta = diffTimeSec(&lastCoderRead, &now) * ABSOLUTE_MAX_VITESSE_ROBOT_CYCP_S;
if (abs(deltaL) > maxDelta) {
deltaL = 0;
}
if (abs(deltaR) > maxDelta) {
deltaR = 0;
}
lastCoderRead = now;
2018-05-11 05:34:06 +02:00
oldL = newL;
oldR = newR;
2018-05-09 14:24:12 +02:00
}
2018-04-04 16:17:13 +02:00
void* TaskPosition(void* pData)
{
(void)pData;
2018-05-06 18:35:26 +02:00
resetPosition();
2018-04-04 16:17:13 +02:00
nbCalcPos = 0;
lCodTot = 0;
rCodTot = 0;
2018-05-09 14:24:12 +02:00
updateDelta();
2018-04-04 16:17:13 +02:00
2018-05-09 14:24:12 +02:00
for (;;) {
2018-04-04 16:17:13 +02:00
2018-05-09 14:24:12 +02:00
updateDelta();
2018-04-04 16:17:13 +02:00
// Calculation
2018-05-06 12:50:03 +02:00
#ifdef INVERSE_L_CODER
2018-05-11 05:34:06 +02:00
deltaL = -deltaL;
2018-05-06 12:50:03 +02:00
#endif
#ifdef INVERSE_R_CODER
2018-05-11 05:34:06 +02:00
deltaR = -deltaR;
2018-05-06 12:50:03 +02:00
#endif
2018-05-11 05:34:06 +02:00
lCodTot += deltaL;
rCodTot += deltaR;
2018-05-02 05:51:14 +02:00
2018-05-11 05:34:06 +02:00
float dR = deltaR * AV_PER_CYCLE;
float dL = deltaL * AV_PER_CYCLE;
2018-05-06 12:50:03 +02:00
float deltaO = atan2(dR - dL, DISTANCE_BETWEEN_WHEELS);
float deltaD = (dL + dR) / 2;
2018-05-02 05:51:14 +02:00
2018-05-06 18:35:26 +02:00
pthread_mutex_lock(&posConnu);
2018-05-02 05:51:14 +02:00
connu.o += deltaO;
float deltaX = deltaD * cos(connu.o);
float deltaY = deltaD * sin(connu.o);
connu.x += deltaX;
connu.y += deltaY;
2018-05-06 18:35:26 +02:00
nbCalcPos++;
pthread_cond_signal(&newPos);
pthread_mutex_unlock(&posConnu);
2018-05-11 15:58:18 +02:00
usleep(POSITION_INTERVAL * 1000);
2018-04-04 16:17:13 +02:00
}
return NULL;
}
void configurePosition()
{
2018-05-11 15:58:18 +02:00
resetPosition();
2018-04-29 09:38:49 +02:00
registerDebugVar("lCodTot", ld, &lCodTot);
registerDebugVar("rCodTot", ld, &rCodTot);
2018-05-11 15:58:18 +02:00
registerDebugVar("newL", d, &newLdbg);
registerDebugVar("newR", d, &newRdbg);
2018-05-02 05:51:14 +02:00
registerDebugVar("xConnu", f, &connu.x);
registerDebugVar("yConnu", f, &connu.y);
registerDebugVar("oConnu", f, &connu.o);
2018-04-29 09:38:49 +02:00
registerDebugVar("nbCalcPos", d, &nbCalcPos);
2018-05-11 15:58:18 +02:00
pthread_mutex_init(&posConnu, NULL);
pthread_cond_init(&newPos, NULL);
2018-04-29 09:38:49 +02:00
pthread_create(&tPosition, NULL, TaskPosition, NULL);
2018-04-04 16:17:13 +02:00
}
void deconfigurePosition()
{
pthread_cancel(tPosition);
}
2018-05-02 08:26:35 +02:00
void getCoders(long* l, long* r)
{
*l = lCodTot;
*r = rCodTot;
2018-05-06 18:35:26 +02:00
}
2018-05-02 08:26:35 +02:00
2018-05-06 18:35:26 +02:00
unsigned int getPosition(struct position* pos)
{
pthread_mutex_lock(&posConnu);
unsigned int nb = nbCalcPos;
memcpy(pos, &connu, sizeof(struct position));
pthread_mutex_unlock(&posConnu);
return nb;
}
unsigned int getPositionNewer(struct position* pos, unsigned int lastCalc)
{
pthread_mutex_lock(&posConnu);
while (nbCalcPos <= lastCalc) {
pthread_cond_wait(&newPos, &posConnu);
}
pthread_mutex_unlock(&posConnu);
return getPosition(pos);
}
void setPosition(struct position* pos)
{
pthread_mutex_lock(&posConnu);
memcpy(&connu, pos, sizeof(struct position));
pthread_mutex_unlock(&posConnu);
}
void resetPosition()
{
2018-05-09 14:24:12 +02:00
struct position pos = { 0, 0, 0 };
2018-05-06 18:35:26 +02:00
setPosition(&pos);
2018-05-02 08:26:35 +02:00
}