Robot secondaire

Dependencies:   RoboClaw mbed StepperMotor

Fork of RoboClaw by Simon Emarre

Odometry/Odometry.cpp

Committer:
sype
Date:
2015-11-24
Revision:
2:abdf8c6823a1
Parent:
0:ad9600df4a70
Child:
3:62e9d715de65

File content as of revision 2:abdf8c6823a1:

#include "Odometry.h"

// M1 = Moteur droit, M2 = Moteur gauche

Odometry::Odometry(double diameter_right, double diameter_left, double v, RoboClaw &rc) : roboclaw(rc)
{
    m_v = v;
    m_distPerTick_left = diameter_left*PI/37400;
    m_distPerTick_right = diameter_right*PI/37400;
    
    erreur_ang = 0.1;
    m_pulses_right = 0;
    m_pulses_left = 0;
    wait_ms(100);
}

void Odometry::setPos(double x, double y, double theta)
{
    this->x = x;
    this->y = y;
    this->theta = theta;
}

void Odometry::setX(double x)
{
    this->x = x;
}

void Odometry::setY(double y)
{
    this->y = y;
}

void Odometry::setTheta(double theta)
{
    this->theta = theta;
}

void Odometry::update_odo(void)
{
    long delta_right = roboclaw.ReadEncM1(ADR) - m_pulses_right;
    m_pulses_right = roboclaw.ReadEncM1(ADR);
    long delta_left = roboclaw.ReadEncM2(ADR) - m_pulses_left;
    m_pulses_left = roboclaw.ReadEncM2(ADR);
    
    double deltaS = (m_distPerTick_left*delta_left + m_distPerTick_right*delta_right) / 2.0f;
    double deltaTheta = (m_distPerTick_right*delta_right - m_distPerTick_left*delta_left)*C / m_v;
    
    double radius = deltaS/deltaTheta;
    double xO = x - radius*sin(theta);
    double yO = y + radius*cos(theta);
    
    theta += deltaTheta;
    
    x = xO + radius*sin(theta);
    y = yO - radius*cos(theta);
    
    while(theta > PI) theta -= 2*PI;
    while(theta <= -PI) theta += 2*PI;
}

void Odometry::GotoXYT(double x_goal, double y_goal, double theta_goal)
{
    double theta_ = atan2(y_goal-y, x_goal-x);
    float distance = sqrt(carre(x_goal-x)+carre(y_goal-y));
    GotoThet(theta_);
}

void Odometry::GotoThet(double theta_)
{
    double distance_ticks_left;
    double distance_ticks_right;
    double erreur_theta = theta_-getTheta();
    
    while(erreur_theta > PI) erreur_theta -= 2*PI;
    while(erreur_theta <= -PI) erreur_theta += 2*PI;
    
    if(erreur_theta >= 0)
    {
        distance_ticks_left = -(erreur_theta*m_v/2)/m_distPerTick_left;
        distance_ticks_right = (erreur_theta*m_v/2)/m_distPerTick_right;
    }
    else
    {
        distance_ticks_left = (erreur_theta*m_v/2)/m_distPerTick_left;
        distance_ticks_right = -(erreur_theta*m_v/2)/m_distPerTick_right;
    }
    
    pc.printf("T_%3.2f\t T%3.2f\t ET%3.2f\n\r",theta_*180/PI, getTheta()*180/PI, erreur_theta*180/PI);
    roboclaw.SpeedAccelDeccelPositionM1M2(ADR, 150000, 150000, 150000, (long)distance_ticks_right, 150000, 150000, 150000, (long)distance_ticks_left, 1);
    while(isArrivedRot(erreur_theta))pc.printf("Theta : %3.2f\n\r",getTheta()*180/PI);;
    pc.printf("Arrived");
}

void Odometry::GotoB(double distance)
{
    double distance_ticks_left = distance/m_distPerTick_left;
    double distance_ticks_right =  distance/m_distPerTick_right;
    roboclaw.SpeedAccelDeccelPositionM1M2(ADR, 150000, 200000, 150000, (long)distance_ticks_right, 150000, 200000, 150000, (long)distance_ticks_left, 1);
}

bool Odometry::isArrivedRot(double theta_)
{
    if((abs_d(getTheta())<=abs_d(theta_)+erreur_ang)|(abs_d(getTheta())>=abs_d(theta_)-erreur_ang)) return true;
    else return false;
}