Mekatronics Team G

Dependencies:   BNO055_fusion PowerControl mbed BMP280

Fork of DEMO3 by Edwin Cho

Committer:
12104404
Date:
Tue Mar 01 19:26:32 2016 +0000
Revision:
1:8966a48ce8d5
Parent:
0:96d6eb224379
Child:
2:634c1adf89b2
XY ANGLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
12104404 0:96d6eb224379 1 #include "LOCALIZE.h"
12104404 0:96d6eb224379 2
12104404 0:96d6eb224379 3 LOCALIZE::LOCALIZE (I2C& y_i2c, I2C& x_imu_i2c, PinName imu_reset):
12104404 1:8966a48ce8d5 4 _y_i2c(y_i2c), _x_imu_i2c(x_imu_i2c), _imu(x_imu_i2c, imu_reset)
12104404 0:96d6eb224379 5 {
12104404 0:96d6eb224379 6
12104404 0:96d6eb224379 7 }
12104404 0:96d6eb224379 8
12104404 0:96d6eb224379 9 int LOCALIZE::init (void)
12104404 0:96d6eb224379 10 {
12104404 0:96d6eb224379 11 if(_imu.chip_ready()==0)
12104404 0:96d6eb224379 12 return ERROR_IMU;
12104404 0:96d6eb224379 13 _imu.set_mounting_position(MT_P0);
12104404 0:96d6eb224379 14 _imu.change_fusion_mode(MODE_IMU);
12104404 1:8966a48ce8d5 15 char data[]= {0,0,0};
12104404 1:8966a48ce8d5 16 if(_x_imu_i2c.read(R_P_ADDR<<1, data, 3)!=0)
12104404 1:8966a48ce8d5 17 return ERROR_RX_P;
12104404 1:8966a48ce8d5 18 if(_y_i2c.read(R_P_ADDR<<1, data, 3)!=0)
12104404 1:8966a48ce8d5 19 return ERROR_RY_P;
12104404 0:96d6eb224379 20 return 0;
12104404 0:96d6eb224379 21 }
12104404 0:96d6eb224379 22
12104404 0:96d6eb224379 23 void LOCALIZE::get_angle(LOCALIZE_xya *xya)
12104404 0:96d6eb224379 24 {
12104404 1:8966a48ce8d5 25 _imu.get_Euler_Angles(&_euler);
12104404 1:8966a48ce8d5 26 _imu.get_gravity(&_gravity);
12104404 1:8966a48ce8d5 27 int _angle=-1;
12104404 0:96d6eb224379 28 unsigned int quad=0;
12104404 1:8966a48ce8d5 29 if(_gravity.y>=0)
12104404 0:96d6eb224379 30 quad&=~1;
12104404 0:96d6eb224379 31 else
12104404 0:96d6eb224379 32 quad|=1;
12104404 1:8966a48ce8d5 33 if(_gravity.x>=0)
12104404 0:96d6eb224379 34 quad&=~(1<<1);
12104404 0:96d6eb224379 35 else
12104404 0:96d6eb224379 36 quad|=(1<<1);
12104404 0:96d6eb224379 37 quad&=3;
12104404 0:96d6eb224379 38 switch(quad) {
12104404 0:96d6eb224379 39 case 0:
12104404 1:8966a48ce8d5 40 _angle=_euler.p;
12104404 0:96d6eb224379 41 break;
12104404 0:96d6eb224379 42 case 1:
12104404 1:8966a48ce8d5 43 _angle=180-_euler.p;
12104404 0:96d6eb224379 44 break;
12104404 0:96d6eb224379 45 case 2:
12104404 1:8966a48ce8d5 46 _angle=360+_euler.p;
12104404 0:96d6eb224379 47 break;
12104404 0:96d6eb224379 48 case 3:
12104404 1:8966a48ce8d5 49 _angle=180+abs(_euler.p);
12104404 0:96d6eb224379 50 break;
12104404 0:96d6eb224379 51 default:
12104404 1:8966a48ce8d5 52 _angle=-1;
12104404 0:96d6eb224379 53 break;
12104404 0:96d6eb224379 54 }
12104404 1:8966a48ce8d5 55 _xya.a=_angle;
12104404 1:8966a48ce8d5 56 xya->a=_xya.a;
12104404 0:96d6eb224379 57 }
12104404 1:8966a48ce8d5 58
12104404 1:8966a48ce8d5 59 void LOCALIZE::get_xy(LOCALIZE_xya *xya)
12104404 1:8966a48ce8d5 60 {
12104404 1:8966a48ce8d5 61 char data[]= {0,0,0};
12104404 1:8966a48ce8d5 62 int _rx_p, _ry_p, _rx, _ry;
12104404 1:8966a48ce8d5 63 _x_imu_i2c.read(R_P_ADDR<<1, data, 3);
12104404 1:8966a48ce8d5 64 //longer wait for longer distance
12104404 1:8966a48ce8d5 65 wait(0.1);
12104404 1:8966a48ce8d5 66 _rx_p=data[1];
12104404 1:8966a48ce8d5 67 _y_i2c.read(R_P_ADDR<<1, data, 3);
12104404 1:8966a48ce8d5 68 wait(0.1);
12104404 1:8966a48ce8d5 69 _ry_p=data[1];
12104404 1:8966a48ce8d5 70 //check angle again
12104404 1:8966a48ce8d5 71 get_angle(xya);
12104404 1:8966a48ce8d5 72 if(_xya.a<R_ERROR || _xya.a>360-R_ERROR) {
12104404 1:8966a48ce8d5 73 _rx=_rx_p;
12104404 1:8966a48ce8d5 74 _ry=_ry_p;
12104404 1:8966a48ce8d5 75 } else if(abs(_xya.a-90)<R_ERROR) {
12104404 1:8966a48ce8d5 76 _rx=_ry_p;
12104404 1:8966a48ce8d5 77 _ry=FRAME_H-RX_OFF-_rx_p;
12104404 1:8966a48ce8d5 78 } else if(abs(_xya.a-180)<R_ERROR) {
12104404 1:8966a48ce8d5 79 _rx=FRAME_W-RX_OFF-_rx_p;
12104404 1:8966a48ce8d5 80 _ry=FRAME_H-RY_OFF-_ry_p;
12104404 1:8966a48ce8d5 81 } else if(abs(_xya.a-270)<R_ERROR) {
12104404 1:8966a48ce8d5 82 _rx=FRAME_W-RY_OFF-_ry_p;
12104404 1:8966a48ce8d5 83 _ry=_rx_p;
12104404 1:8966a48ce8d5 84 } else {
12104404 1:8966a48ce8d5 85 _rx=_rx_p;
12104404 1:8966a48ce8d5 86 _ry=_ry_p;
12104404 1:8966a48ce8d5 87 }
12104404 1:8966a48ce8d5 88 _xya.x=_rx;
12104404 1:8966a48ce8d5 89 _xya.y=_ry;
12104404 1:8966a48ce8d5 90 xya->x=_xya.x;
12104404 1:8966a48ce8d5 91 xya->y=_xya.y;
12104404 1:8966a48ce8d5 92 }
12104404 1:8966a48ce8d5 93