cansat-d_2018 / HMC5883L
Committer:
394
Date:
Fri Oct 19 09:20:17 2018 +0000
Revision:
5:489c2f8770da
Parent:
4:bc4e1201e092
Child:
6:48004ffd01c5
test program for groundsensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:8b84d61cee94 1 #include "HMC5883L.h"
tylerjw 0:8b84d61cee94 2 #include <new>
tylerjw 0:8b84d61cee94 3
394 5:489c2f8770da 4 HMC5883L::HMC5883L(PinName p28, PinName p27) : i2c_(*reinterpret_cast<I2C*>(i2cRaw))
tylerjw 0:8b84d61cee94 5 {
394 5:489c2f8770da 6 new(i2cRaw) I2C(p28, p27);
tylerjw 0:8b84d61cee94 7 init();
tylerjw 0:8b84d61cee94 8 }
tylerjw 0:8b84d61cee94 9
tylerjw 0:8b84d61cee94 10 HMC5883L::~HMC5883L()
tylerjw 0:8b84d61cee94 11 {
tylerjw 0:8b84d61cee94 12 if(&i2c_ == reinterpret_cast<I2C*>(&i2cRaw))
tylerjw 0:8b84d61cee94 13 reinterpret_cast<I2C*>(&i2cRaw)->~I2C();
tylerjw 0:8b84d61cee94 14 }
tylerjw 0:8b84d61cee94 15 void HMC5883L::init()
tylerjw 0:8b84d61cee94 16 {
tylerjw 0:8b84d61cee94 17 // init - configure your setup here
tylerjw 0:8b84d61cee94 18 setConfigurationA(AVG8_SAMPLES | OUTPUT_RATE_15); // 8 sample average, 15Hz, normal mode
tylerjw 0:8b84d61cee94 19 setConfigurationB(0x20); // default
tylerjw 0:8b84d61cee94 20 setMode(CONTINUOUS_MODE); // continuous sample mode
tylerjw 0:8b84d61cee94 21 }
tylerjw 0:8b84d61cee94 22
tylerjw 0:8b84d61cee94 23 void HMC5883L::setConfigurationA(char config)
tylerjw 0:8b84d61cee94 24 {
tylerjw 0:8b84d61cee94 25 char cmd[2];
tylerjw 0:8b84d61cee94 26 cmd[0] = CONFIG_A_REG; // register a address
tylerjw 0:8b84d61cee94 27 cmd[1] = config;
tylerjw 0:8b84d61cee94 28 i2c_.write(I2C_ADDRESS, cmd, 2);
tylerjw 0:8b84d61cee94 29 }
tylerjw 0:8b84d61cee94 30
tylerjw 0:8b84d61cee94 31 void HMC5883L::setConfigurationB(char config)
tylerjw 0:8b84d61cee94 32 {
tylerjw 0:8b84d61cee94 33 char cmd[2];
tylerjw 0:8b84d61cee94 34 cmd[0] = CONFIG_B_REG; // register b address
tylerjw 0:8b84d61cee94 35 cmd[1] = config;
tylerjw 0:8b84d61cee94 36 i2c_.write(I2C_ADDRESS, cmd, 2);
tylerjw 0:8b84d61cee94 37 }
tylerjw 0:8b84d61cee94 38
tylerjw 0:8b84d61cee94 39 char HMC5883L::getConfigurationA()
tylerjw 0:8b84d61cee94 40 {
tylerjw 0:8b84d61cee94 41 char cmd[2];
tylerjw 0:8b84d61cee94 42 cmd[0] = CONFIG_A_REG; // register a address
tylerjw 0:8b84d61cee94 43 i2c_.write(I2C_ADDRESS, cmd, 1, true);
tylerjw 0:8b84d61cee94 44 i2c_.read(I2C_ADDRESS, &cmd[1], 1, false);
tylerjw 0:8b84d61cee94 45 return cmd[1];
tylerjw 0:8b84d61cee94 46 }
tylerjw 0:8b84d61cee94 47
tylerjw 0:8b84d61cee94 48 char HMC5883L::getConfigurationB()
tylerjw 0:8b84d61cee94 49 {
tylerjw 0:8b84d61cee94 50 char cmd[2];
394 5:489c2f8770da 51 cmd[0] = CONFIG_B_REG; // register b address
tylerjw 0:8b84d61cee94 52 i2c_.write(I2C_ADDRESS, cmd, 1, true);
tylerjw 0:8b84d61cee94 53 i2c_.read(I2C_ADDRESS, &cmd[1], 1, false);
tylerjw 0:8b84d61cee94 54 return cmd[1];
tylerjw 0:8b84d61cee94 55 }
tylerjw 0:8b84d61cee94 56
tylerjw 0:8b84d61cee94 57 void HMC5883L::setMode(char mode = SINGLE_MODE)
tylerjw 0:8b84d61cee94 58 {
tylerjw 0:8b84d61cee94 59 char cmd[2];
tylerjw 0:8b84d61cee94 60 cmd[0] = MODE_REG; // mode register address
tylerjw 0:8b84d61cee94 61 cmd[1] = mode;
tylerjw 0:8b84d61cee94 62 i2c_.write(I2C_ADDRESS,cmd,2);
tylerjw 0:8b84d61cee94 63 }
tylerjw 0:8b84d61cee94 64
tylerjw 0:8b84d61cee94 65 char HMC5883L::getMode()
tylerjw 0:8b84d61cee94 66 {
tylerjw 0:8b84d61cee94 67 char cmd[2];
tylerjw 0:8b84d61cee94 68 cmd[0] = MODE_REG; // mode register
tylerjw 0:8b84d61cee94 69 i2c_.write(I2C_ADDRESS, cmd, 1, true);
tylerjw 0:8b84d61cee94 70 i2c_.read(I2C_ADDRESS, &cmd[1], 1, false);
tylerjw 0:8b84d61cee94 71 return cmd[1];
tylerjw 0:8b84d61cee94 72 }
tylerjw 0:8b84d61cee94 73
tylerjw 0:8b84d61cee94 74 char HMC5883L::getStatus()
tylerjw 0:8b84d61cee94 75 {
tylerjw 0:8b84d61cee94 76 char cmd[2];
tylerjw 0:8b84d61cee94 77 cmd[0] = STATUS_REG; // status register
tylerjw 0:8b84d61cee94 78 i2c_.write(I2C_ADDRESS, cmd, 1, true);
tylerjw 0:8b84d61cee94 79 i2c_.read(I2C_ADDRESS, &cmd[1], 1, false);
tylerjw 0:8b84d61cee94 80 return cmd[1];
tylerjw 0:8b84d61cee94 81 }
tylerjw 0:8b84d61cee94 82
394 5:489c2f8770da 83 void HMC5883L::getXYZ(int16_t output[3])//データの取得
tylerjw 0:8b84d61cee94 84 {
tylerjw 0:8b84d61cee94 85 char cmd[2];
tylerjw 0:8b84d61cee94 86 char data[6];
tylerjw 0:8b84d61cee94 87 cmd[0] = 0x03; // starting point for reading
tylerjw 0:8b84d61cee94 88 i2c_.write(I2C_ADDRESS, cmd, 1, true); // set the pointer to the start of x
tylerjw 0:8b84d61cee94 89 i2c_.read(I2C_ADDRESS, data, 6, false);
tylerjw 0:8b84d61cee94 90 for(int i = 0; i < 3; i++) // fill the output variables
tylerjw 0:8b84d61cee94 91 output[i] = int16_t(((unsigned char)data[i*2] << 8) | (unsigned char)data[i*2+1]);
tylerjw 1:8a1357c351c6 92 }
tylerjw 1:8a1357c351c6 93
tylerjw 1:8a1357c351c6 94 double HMC5883L::getHeadingXY()
tylerjw 1:8a1357c351c6 95 {
tylerjw 4:bc4e1201e092 96 int16_t raw_data[3];
tylerjw 1:8a1357c351c6 97 getXYZ(raw_data);
394 5:489c2f8770da 98 double heading = atan2(static_cast<double>(raw_data[2]), static_cast<double>(raw_data[0])); // heading = arctan(Y/X) (Y/X)の逆正接
394 5:489c2f8770da 99 // 角度範囲の補正
tylerjw 1:8a1357c351c6 100 if(heading < 0.0) // fix sign
tylerjw 2:8eb755577f83 101 heading += PI2;
tylerjw 2:8eb755577f83 102 if(heading > PI2) // fix overflow
tylerjw 2:8eb755577f83 103 heading -= PI2;
tylerjw 1:8a1357c351c6 104 return heading;
tylerjw 0:8b84d61cee94 105 }