![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
added
Dependencies: BNO055_fusion PowerControl mbed
Fork of TEAM_G_FLOW_RIDA by
LOCALIZE.cpp@4:d70375cfa533, 2016-03-04 (annotated)
- Committer:
- 12104404
- Date:
- Fri Mar 04 03:02:36 2016 +0000
- Revision:
- 4:d70375cfa533
- Parent:
- 3:c58eb4be51de
- Child:
- 5:c89308dc1827
added N P;
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:d70375cfa533 | 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 | int LOCALIZE::init (void) |
12104404 | 0:96d6eb224379 | 7 | { |
12104404 | 0:96d6eb224379 | 8 | if(_imu.chip_ready()==0) |
12104404 | 0:96d6eb224379 | 9 | return ERROR_IMU; |
12104404 | 0:96d6eb224379 | 10 | _imu.set_mounting_position(MT_P0); |
12104404 | 0:96d6eb224379 | 11 | _imu.change_fusion_mode(MODE_IMU); |
12104404 | 1:8966a48ce8d5 | 12 | char data[]= {0,0,0}; |
12104404 | 1:8966a48ce8d5 | 13 | if(_x_imu_i2c.read(R_P_ADDR<<1, data, 3)!=0) |
12104404 | 1:8966a48ce8d5 | 14 | return ERROR_RX_P; |
12104404 | 2:634c1adf89b2 | 15 | if(_x_imu_i2c.read(R_N_ADDR<<1, data, 3)!=0) |
12104404 | 2:634c1adf89b2 | 16 | return ERROR_RX_N; |
12104404 | 1:8966a48ce8d5 | 17 | if(_y_i2c.read(R_P_ADDR<<1, data, 3)!=0) |
12104404 | 1:8966a48ce8d5 | 18 | return ERROR_RY_P; |
12104404 | 2:634c1adf89b2 | 19 | if(_y_i2c.read(R_N_ADDR<<1, data, 3)!=0) |
12104404 | 2:634c1adf89b2 | 20 | return ERROR_RY_N; |
12104404 | 0:96d6eb224379 | 21 | return 0; |
12104404 | 0:96d6eb224379 | 22 | } |
12104404 | 0:96d6eb224379 | 23 | |
12104404 | 0:96d6eb224379 | 24 | void LOCALIZE::get_angle(LOCALIZE_xya *xya) |
12104404 | 0:96d6eb224379 | 25 | { |
12104404 | 1:8966a48ce8d5 | 26 | _imu.get_Euler_Angles(&_euler); |
12104404 | 1:8966a48ce8d5 | 27 | _imu.get_gravity(&_gravity); |
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 | 3:c58eb4be51de | 61 | int _rx, _ry; |
12104404 | 3:c58eb4be51de | 62 | get_raw_xy(); |
12104404 | 1:8966a48ce8d5 | 63 | get_angle(xya); |
12104404 | 1:8966a48ce8d5 | 64 | if(_xya.a<R_ERROR || _xya.a>360-R_ERROR) { |
12104404 | 4:d70375cfa533 | 65 | _rx=_rx_p<_rx_n ? _rx_p : FRAME_W-RX_OFF-_rx_n; |
12104404 | 4:d70375cfa533 | 66 | _ry=_ry_p<_ry_n ? _ry_p : FRAME_H-RY_OFF-_ry_n; |
12104404 | 1:8966a48ce8d5 | 67 | } else if(abs(_xya.a-90)<R_ERROR) { |
12104404 | 4:d70375cfa533 | 68 | _rx=_ry_p<_ry_n ? _ry_p : FRAME_W-RY_OFF-_ry_n; |
12104404 | 4:d70375cfa533 | 69 | _ry=_rx_p<_rx_n ? FRAME_H-RX_OFF-_rx_p : _rx_n; |
12104404 | 1:8966a48ce8d5 | 70 | } else if(abs(_xya.a-180)<R_ERROR) { |
12104404 | 4:d70375cfa533 | 71 | _rx=_rx_p<_rx_n ? FRAME_W-RX_OFF-_rx_p : _rx_n; |
12104404 | 4:d70375cfa533 | 72 | _ry=_ry_p<_ry_n ? FRAME_H-RY_OFF-_ry_p : _ry_n; |
12104404 | 1:8966a48ce8d5 | 73 | } else if(abs(_xya.a-270)<R_ERROR) { |
12104404 | 4:d70375cfa533 | 74 | _rx=_ry_p<_ry_n ? FRAME_W-RY_OFF-_ry_p : _ry_n; |
12104404 | 4:d70375cfa533 | 75 | _ry=_rx_p<_rx_n ? _rx_p : FRAME_H-RX_OFF-_rx_n; |
12104404 | 1:8966a48ce8d5 | 76 | } else { |
12104404 | 4:d70375cfa533 | 77 | //error |
12104404 | 1:8966a48ce8d5 | 78 | _rx=_rx_p; |
12104404 | 1:8966a48ce8d5 | 79 | _ry=_ry_p; |
12104404 | 1:8966a48ce8d5 | 80 | } |
12104404 | 1:8966a48ce8d5 | 81 | _xya.x=_rx; |
12104404 | 1:8966a48ce8d5 | 82 | _xya.y=_ry; |
12104404 | 1:8966a48ce8d5 | 83 | xya->x=_xya.x; |
12104404 | 1:8966a48ce8d5 | 84 | xya->y=_xya.y; |
12104404 | 1:8966a48ce8d5 | 85 | } |
12104404 | 1:8966a48ce8d5 | 86 | |
12104404 | 3:c58eb4be51de | 87 | void LOCALIZE::get_raw_xy(void) |
12104404 | 3:c58eb4be51de | 88 | { |
12104404 | 3:c58eb4be51de | 89 | char data[]= {0,0,0}; |
12104404 | 3:c58eb4be51de | 90 | _x_imu_i2c.read(R_P_ADDR<<1, data, 3); |
12104404 | 4:d70375cfa533 | 91 | wait(R_WAIT); |
12104404 | 3:c58eb4be51de | 92 | _rx_p=data[1]; |
12104404 | 3:c58eb4be51de | 93 | _x_imu_i2c.read(R_N_ADDR<<1, data, 3); |
12104404 | 4:d70375cfa533 | 94 | wait(R_WAIT); |
12104404 | 3:c58eb4be51de | 95 | _rx_n=data[1]; |
12104404 | 3:c58eb4be51de | 96 | _y_i2c.read(R_P_ADDR<<1, data, 3); |
12104404 | 4:d70375cfa533 | 97 | wait(R_WAIT); |
12104404 | 3:c58eb4be51de | 98 | _ry_p=data[1]; |
12104404 | 3:c58eb4be51de | 99 | _y_i2c.read(R_N_ADDR<<1, data, 3); |
12104404 | 4:d70375cfa533 | 100 | wait(R_WAIT); |
12104404 | 3:c58eb4be51de | 101 | _ry_n=data[1]; |
12104404 | 3:c58eb4be51de | 102 | } |