mb_odometry.c 3.54 KB
Newer Older
pgaskell's avatar
pgaskell committed
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
* mb_odometry.c
*
* TODO: Implement these functions to add odometry and dead rekoning 
*
*******************************************************************************/

#include "../mobilebot/mobilebot.h"
#include "mb_defs.h"
#include <math.h>
jxchenlu's avatar
jxchenlu committed
11
12
#include <stdlib.h>
typedef enum { F, T } boolean;
pgaskell's avatar
pgaskell committed
13
#define PI 3.14159265358979323846
Demo User's avatar
Demo User committed
14
//float enc2meters = (WHEEL_DIAMETER * PI) / (GEAR_RATIO * ENCODER_RES);
pgaskell's avatar
pgaskell committed
15
16
17
18
19
20
21
22
/*******************************************************************************
* mb_initialize_odometry() 
*
* TODO: initialize odometry
* NOTE: you should initialize from Optitrack data if available
*
*******************************************************************************/
void mb_initialize_odometry(mb_odometry_t* mb_odometry, float x, float y, float theta){
jxchenlu's avatar
jxchenlu committed
23
24
25
    mb_odometry->x = x;
    mb_odometry->y = y;
    mb_odometry->theta = theta;
pgaskell's avatar
pgaskell committed
26
27
28
29
30
31
32
33
34
35
36
}


/*******************************************************************************
* mb_update_odometry() 
*
* TODO: calculate odometry from internal variables
*       publish new odometry to lcm ODOMETRY_CHANNEL
*
*******************************************************************************/
void mb_update_odometry(mb_odometry_t* mb_odometry, mb_state_t* mb_state){
Demo User's avatar
Demo User committed
37
    // mb_odometry is the input of previenc2metersous veh's position and orientation
jxchenlu's avatar
jxchenlu committed
38
    // Reference: https://www.hmc.edu/lair/ARW/ARW-Lecture01-Odometry.pdf  P28
jxchenlu's avatar
jxchenlu committed
39
40
    //del_thresh Needs to be tuned
    //IF DON'T WANT GYRO, SET IT AS F!
Demo User's avatar
Demo User committed
41
    float enc2meters = (WHEEL_DIAMETER * PI) / (GEAR_RATIO * ENCODER_RES);
jxchenlu's avatar
jxchenlu committed
42
43
    float del_thresh = 0.125/DT; 
    printf("del_thresh", del_thresh);
jxchenlu's avatar
jxchenlu committed
44
45
    boolean Gyro = T;

jxchenlu's avatar
jxchenlu committed
46
47
    float del_left = mb_state->left_encoder_delta * 1.0 * enc2meters;
    float del_right = mb_state->right_encoder_delta * 1.0 * enc2meters;
jxchenlu's avatar
jxchenlu committed
48
49
    float del_s = (del_right + del_left) / 2;//float del_s = (del_left + del_right) / 2;
    float del_theta = mb_clamp_radians((del_right - del_left) / WHEEL_BASE);
jxchenlu's avatar
jxchenlu committed
50
51
52
53
    float del_x = del_s * cos(mb_odometry->theta + del_theta/2);
    float del_y = del_s * sin(mb_odometry->theta + del_theta/2);
    mb_odometry->x += del_x;
    mb_odometry->y += del_y;
jxchenlu's avatar
jxchenlu committed
54
55
56
57


    // Gyro: we only need to use yaw
    // Since each time step is small, no need to integrate
jxchenlu's avatar
jxchenlu committed
58
    float del_theta_gyro = mb_clamp_radians(mb_state->gyro[2] * (PI / 180.0) * DT);
jxchenlu's avatar
jxchenlu committed
59
60
    float del_go = del_theta_gyro - del_theta;
    if (Gyro == T){
jxchenlu's avatar
jxchenlu committed
61
        if (abs(del_go) >= 0.0){mb_odometry->theta += del_theta_gyro * DT;}
jxchenlu's avatar
jxchenlu committed
62
63
64
65
66
        else{mb_odometry->theta += del_theta * DT;}
    }
    else{// this is after we update the del_x and y 
        mb_odometry->theta += del_theta;}
    mb_odometry->theta = mb_clamp_radians(mb_odometry->theta); 
pgaskell's avatar
pgaskell committed
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
}


/*******************************************************************************
* mb_clamp_radians() 
* clamp an angle from -PI to PI
*******************************************************************************/
float mb_clamp_radians(float angle){

    if(angle < -PI)
    {
        for(; angle < -PI; angle += 2.0*PI);
    }
    else if(angle > PI)
    {
        for(; angle > PI; angle -= 2.0*PI);
    }

    return angle;
}


/*******************************************************************************
* mb_angle_diff_radians() 
* computes difference between 2 angles and wraps from -PI to PI
*******************************************************************************/
float mb_angle_diff_radians(float angle1, float angle2){
    float diff = angle2 - angle1;
    while(diff < -PI) diff+=2.0*PI;
    while(diff > PI) diff-=2.0*PI;
    return diff;
}