Refactoring Ironcup 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
starling
Date:
Mon Sep 21 21:42:07 2020 +0000
Revision:
0:8f5db5085df7
12 mar 2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
starling 0:8f5db5085df7 1 /* Copyright (c) 2015 NXP Semiconductors. MIT License
starling 0:8f5db5085df7 2 *
starling 0:8f5db5085df7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
starling 0:8f5db5085df7 4 * and associated documentation files (the "Software"), to deal in the Software without
starling 0:8f5db5085df7 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
starling 0:8f5db5085df7 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
starling 0:8f5db5085df7 7 * Software is furnished to do so, subject to the following conditions:
starling 0:8f5db5085df7 8 *
starling 0:8f5db5085df7 9 * The above copyright notice and this permission notice shall be included in all copies or
starling 0:8f5db5085df7 10 * substantial portions of the Software.
starling 0:8f5db5085df7 11 *
starling 0:8f5db5085df7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
starling 0:8f5db5085df7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
starling 0:8f5db5085df7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
starling 0:8f5db5085df7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
starling 0:8f5db5085df7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
starling 0:8f5db5085df7 17 */
starling 0:8f5db5085df7 18
starling 0:8f5db5085df7 19 #include "FXAS21002.h"
starling 0:8f5db5085df7 20 #include "mbed.h"
starling 0:8f5db5085df7 21 FXAS21002::FXAS21002(PinName sda, PinName scl) : gyroi2c(sda,scl)
starling 0:8f5db5085df7 22 {
starling 0:8f5db5085df7 23
starling 0:8f5db5085df7 24 }
starling 0:8f5db5085df7 25
starling 0:8f5db5085df7 26 void FXAS21002::set_gyro(gyro_mode mode) // Protected method
starling 0:8f5db5085df7 27 {
starling 0:8f5db5085df7 28 char d[2];
starling 0:8f5db5085df7 29 d[0] = FXAS21002_CTRL_REG1; //Puts device in standby mode
starling 0:8f5db5085df7 30 d[1] = 0x08;
starling 0:8f5db5085df7 31 gyroi2c.write(FXAS21002_I2C_ADDRESS, d,2);
starling 0:8f5db5085df7 32
starling 0:8f5db5085df7 33 d[0] = FXAS21002_CTRL_REG0; //Sets FSR and Sensitivity
starling 0:8f5db5085df7 34 d[1] = mode + 0x80;
starling 0:8f5db5085df7 35 gyroi2c.write(FXAS21002_I2C_ADDRESS, d, 2);
starling 0:8f5db5085df7 36
starling 0:8f5db5085df7 37 d[0] = FXAS21002_CTRL_REG1; //Puts device in active mode
starling 0:8f5db5085df7 38 d[1] = 0x0A;
starling 0:8f5db5085df7 39 gyroi2c.write(FXAS21002_I2C_ADDRESS, d,2);
starling 0:8f5db5085df7 40 }
starling 0:8f5db5085df7 41
starling 0:8f5db5085df7 42
starling 0:8f5db5085df7 43 void FXAS21002::stop_measure(void)
starling 0:8f5db5085df7 44 {
starling 0:8f5db5085df7 45 interrupt.detach();
starling 0:8f5db5085df7 46 angle = 0;
starling 0:8f5db5085df7 47 }
starling 0:8f5db5085df7 48
starling 0:8f5db5085df7 49 float FXAS21002::get_angle(void)
starling 0:8f5db5085df7 50 {
starling 0:8f5db5085df7 51 return angle;
starling 0:8f5db5085df7 52 }
starling 0:8f5db5085df7 53
starling 0:8f5db5085df7 54 void FXAS21002::integrate_gyro_angle(void)
starling 0:8f5db5085df7 55 {
starling 0:8f5db5085df7 56 float gyro_data[3];
starling 0:8f5db5085df7 57 acquire_gyro_data_dps(gyro_data);
starling 0:8f5db5085df7 58
starling 0:8f5db5085df7 59 angle = angle + (gyro_data[2]-GYRO_OFFSET)*(period/1000000);
starling 0:8f5db5085df7 60 if(angle > 180)
starling 0:8f5db5085df7 61 angle = angle - 360;
starling 0:8f5db5085df7 62 if(angle < -180)
starling 0:8f5db5085df7 63 angle = angle + 360;
starling 0:8f5db5085df7 64 }
starling 0:8f5db5085df7 65
starling 0:8f5db5085df7 66 void FXAS21002::start_measure(float period_us)
starling 0:8f5db5085df7 67 {
starling 0:8f5db5085df7 68 period = period_us;
starling 0:8f5db5085df7 69 interrupt.attach_us(this,&FXAS21002::integrate_gyro_angle,period);
starling 0:8f5db5085df7 70 angle = 0;
starling 0:8f5db5085df7 71 }
starling 0:8f5db5085df7 72
starling 0:8f5db5085df7 73 void FXAS21002::gyro_config(void)
starling 0:8f5db5085df7 74 {
starling 0:8f5db5085df7 75 set_gyro(MODE_2); //Default implementation
starling 0:8f5db5085df7 76 sensitivity = 0.03125;
starling 0:8f5db5085df7 77 }
starling 0:8f5db5085df7 78
starling 0:8f5db5085df7 79 void FXAS21002::gyro_config(gyro_mode mode)
starling 0:8f5db5085df7 80 {
starling 0:8f5db5085df7 81 set_gyro(mode);
starling 0:8f5db5085df7 82 switch(mode)
starling 0:8f5db5085df7 83 {
starling 0:8f5db5085df7 84 case MODE_1: sensitivity = 0.0625; break;
starling 0:8f5db5085df7 85 case MODE_2: sensitivity = 0.03125; break;
starling 0:8f5db5085df7 86 case MODE_3: sensitivity = 0.015625; break;
starling 0:8f5db5085df7 87 case MODE_4: sensitivity = 0.0078125; break;
starling 0:8f5db5085df7 88 }
starling 0:8f5db5085df7 89 }
starling 0:8f5db5085df7 90
starling 0:8f5db5085df7 91 void FXAS21002::acquire_gyro_data_dps(float * g_data)
starling 0:8f5db5085df7 92 {
starling 0:8f5db5085df7 93
starling 0:8f5db5085df7 94 char data_bytes[7];
starling 0:8f5db5085df7 95 gyroi2c.write(FXAS21002_I2C_ADDRESS,FXAS21002_STATUS,1,true); // Read the 6 data bytes - LSB and MSB for X, Y and Z Axes.
starling 0:8f5db5085df7 96 gyroi2c.read(FXAS21002_I2C_ADDRESS,data_bytes,7);
starling 0:8f5db5085df7 97
starling 0:8f5db5085df7 98 g_data[0] = (float)((int16_t)((data_bytes[1]*256) + (data_bytes[2]))) * sensitivity;
starling 0:8f5db5085df7 99 g_data[1] = (float)((int16_t)((data_bytes[3]*256) + (data_bytes[4]))) * sensitivity;
starling 0:8f5db5085df7 100 g_data[2] = (float)((int16_t)((data_bytes[5]*256) + (data_bytes[6]))) * sensitivity;
starling 0:8f5db5085df7 101
starling 0:8f5db5085df7 102 }
starling 0:8f5db5085df7 103
starling 0:8f5db5085df7 104
starling 0:8f5db5085df7 105
starling 0:8f5db5085df7 106