Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
Sensors/HMC5843/HMC5843.cpp@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 0:a6a169de725f
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | /** |
shimniok | 0:a6a169de725f | 2 | * @author Jose R. Padron |
shimniok | 0:a6a169de725f | 3 | * @author Used HMC6352 library developed by Aaron Berk as template |
shimniok | 0:a6a169de725f | 4 | * @section LICENSE |
shimniok | 0:a6a169de725f | 5 | * |
shimniok | 0:a6a169de725f | 6 | * Copyright (c) 2010 ARM Limited |
shimniok | 0:a6a169de725f | 7 | * |
shimniok | 0:a6a169de725f | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
shimniok | 0:a6a169de725f | 9 | * of this software and associated documentation files (the "Software"), to deal |
shimniok | 0:a6a169de725f | 10 | * in the Software without restriction, including without limitation the rights |
shimniok | 0:a6a169de725f | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
shimniok | 0:a6a169de725f | 12 | * copies of the Software, and to permit persons to whom the Software is |
shimniok | 0:a6a169de725f | 13 | * furnished to do so, subject to the following conditions: |
shimniok | 0:a6a169de725f | 14 | * |
shimniok | 0:a6a169de725f | 15 | * The above copyright notice and this permission notice shall be included in |
shimniok | 0:a6a169de725f | 16 | * all copies or substantial portions of the Software. |
shimniok | 0:a6a169de725f | 17 | * |
shimniok | 0:a6a169de725f | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
shimniok | 0:a6a169de725f | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
shimniok | 0:a6a169de725f | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
shimniok | 0:a6a169de725f | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
shimniok | 0:a6a169de725f | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
shimniok | 0:a6a169de725f | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
shimniok | 0:a6a169de725f | 24 | * THE SOFTWARE. |
shimniok | 0:a6a169de725f | 25 | * |
shimniok | 0:a6a169de725f | 26 | * @section DESCRIPTION |
shimniok | 0:a6a169de725f | 27 | * |
shimniok | 0:a6a169de725f | 28 | * Honeywell HMC5843digital compass. |
shimniok | 0:a6a169de725f | 29 | * |
shimniok | 0:a6a169de725f | 30 | * Datasheet: |
shimniok | 0:a6a169de725f | 31 | * |
shimniok | 0:a6a169de725f | 32 | * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf |
shimniok | 0:a6a169de725f | 33 | */ |
shimniok | 0:a6a169de725f | 34 | |
shimniok | 0:a6a169de725f | 35 | /** |
shimniok | 0:a6a169de725f | 36 | * Includes |
shimniok | 0:a6a169de725f | 37 | */ |
shimniok | 0:a6a169de725f | 38 | #include "HMC5843.h" |
shimniok | 0:a6a169de725f | 39 | |
shimniok | 0:a6a169de725f | 40 | HMC5843::HMC5843(PinName sda, PinName scl) |
shimniok | 0:a6a169de725f | 41 | { |
shimniok | 0:a6a169de725f | 42 | i2c_ = new I2C(sda, scl); |
shimniok | 0:a6a169de725f | 43 | //100KHz, as specified by the datasheet. |
shimniok | 0:a6a169de725f | 44 | i2c_->frequency(100000); |
shimniok | 0:a6a169de725f | 45 | setOpMode(HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL, HMC5843_1_0GA); |
shimniok | 0:a6a169de725f | 46 | |
shimniok | 0:a6a169de725f | 47 | return; |
shimniok | 0:a6a169de725f | 48 | } |
shimniok | 0:a6a169de725f | 49 | |
shimniok | 0:a6a169de725f | 50 | |
shimniok | 0:a6a169de725f | 51 | void HMC5843::setOffset(float x, float y, float z) |
shimniok | 0:a6a169de725f | 52 | { |
shimniok | 0:a6a169de725f | 53 | return; |
shimniok | 0:a6a169de725f | 54 | } |
shimniok | 0:a6a169de725f | 55 | |
shimniok | 0:a6a169de725f | 56 | |
shimniok | 0:a6a169de725f | 57 | void HMC5843::setScale(float x, float y, float z) |
shimniok | 0:a6a169de725f | 58 | { |
shimniok | 0:a6a169de725f | 59 | return; |
shimniok | 0:a6a169de725f | 60 | } |
shimniok | 0:a6a169de725f | 61 | |
shimniok | 0:a6a169de725f | 62 | |
shimniok | 0:a6a169de725f | 63 | void HMC5843::setSleepMode() |
shimniok | 0:a6a169de725f | 64 | { |
shimniok | 0:a6a169de725f | 65 | write(HMC5843_MODE, HMC5843_SLEEP); |
shimniok | 0:a6a169de725f | 66 | |
shimniok | 0:a6a169de725f | 67 | return; |
shimniok | 0:a6a169de725f | 68 | } |
shimniok | 0:a6a169de725f | 69 | |
shimniok | 0:a6a169de725f | 70 | |
shimniok | 0:a6a169de725f | 71 | void HMC5843::setDefault(void) |
shimniok | 0:a6a169de725f | 72 | { |
shimniok | 0:a6a169de725f | 73 | write(HMC5843_CONFIG_A,HMC5843_10HZ_NORMAL); |
shimniok | 0:a6a169de725f | 74 | write(HMC5843_CONFIG_B,HMC5843_1_0GA); |
shimniok | 0:a6a169de725f | 75 | write(HMC5843_MODE,HMC5843_CONTINUOUS); |
shimniok | 0:a6a169de725f | 76 | wait_ms(100); |
shimniok | 0:a6a169de725f | 77 | |
shimniok | 0:a6a169de725f | 78 | return; |
shimniok | 0:a6a169de725f | 79 | } |
shimniok | 0:a6a169de725f | 80 | |
shimniok | 0:a6a169de725f | 81 | |
shimniok | 0:a6a169de725f | 82 | void HMC5843::setOpMode(int mode, int ConfigA, int ConfigB) |
shimniok | 0:a6a169de725f | 83 | { |
shimniok | 0:a6a169de725f | 84 | write(HMC5843_CONFIG_A,ConfigA); |
shimniok | 0:a6a169de725f | 85 | write(HMC5843_CONFIG_B,ConfigB); |
shimniok | 0:a6a169de725f | 86 | write(HMC5843_MODE,mode); |
shimniok | 0:a6a169de725f | 87 | |
shimniok | 0:a6a169de725f | 88 | return; |
shimniok | 0:a6a169de725f | 89 | } |
shimniok | 0:a6a169de725f | 90 | |
shimniok | 0:a6a169de725f | 91 | |
shimniok | 0:a6a169de725f | 92 | void HMC5843::write(int address, int data) |
shimniok | 0:a6a169de725f | 93 | { |
shimniok | 0:a6a169de725f | 94 | char tx[2]; |
shimniok | 0:a6a169de725f | 95 | |
shimniok | 0:a6a169de725f | 96 | tx[0]=address; |
shimniok | 0:a6a169de725f | 97 | tx[1]=data; |
shimniok | 0:a6a169de725f | 98 | |
shimniok | 0:a6a169de725f | 99 | i2c_->write(HMC5843_I2C_WRITE,tx,2); |
shimniok | 0:a6a169de725f | 100 | |
shimniok | 0:a6a169de725f | 101 | wait_ms(100); |
shimniok | 0:a6a169de725f | 102 | |
shimniok | 0:a6a169de725f | 103 | return; |
shimniok | 0:a6a169de725f | 104 | } |
shimniok | 0:a6a169de725f | 105 | |
shimniok | 0:a6a169de725f | 106 | |
shimniok | 0:a6a169de725f | 107 | void HMC5843::read(int m[3]) |
shimniok | 0:a6a169de725f | 108 | { |
shimniok | 0:a6a169de725f | 109 | readData(m); |
shimniok | 0:a6a169de725f | 110 | |
shimniok | 0:a6a169de725f | 111 | return; |
shimniok | 0:a6a169de725f | 112 | } |
shimniok | 0:a6a169de725f | 113 | |
shimniok | 0:a6a169de725f | 114 | void HMC5843::readMag(int m[3]) |
shimniok | 0:a6a169de725f | 115 | { |
shimniok | 0:a6a169de725f | 116 | readData(m); |
shimniok | 0:a6a169de725f | 117 | |
shimniok | 0:a6a169de725f | 118 | return; |
shimniok | 0:a6a169de725f | 119 | } |
shimniok | 0:a6a169de725f | 120 | |
shimniok | 0:a6a169de725f | 121 | void HMC5843::readData(int readings[3]) |
shimniok | 0:a6a169de725f | 122 | { |
shimniok | 0:a6a169de725f | 123 | char tx[1]; |
shimniok | 0:a6a169de725f | 124 | char rx[2]; |
shimniok | 0:a6a169de725f | 125 | |
shimniok | 0:a6a169de725f | 126 | |
shimniok | 0:a6a169de725f | 127 | tx[0]=HMC5843_X_MSB; |
shimniok | 0:a6a169de725f | 128 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 129 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 130 | readings[0]= (short)(rx[0]<<8|rx[1]); |
shimniok | 0:a6a169de725f | 131 | |
shimniok | 0:a6a169de725f | 132 | |
shimniok | 0:a6a169de725f | 133 | tx[0]=HMC5843_Y_MSB; |
shimniok | 0:a6a169de725f | 134 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 135 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 136 | readings[1]= (short) (rx[0]<<8|rx[1]); |
shimniok | 0:a6a169de725f | 137 | |
shimniok | 0:a6a169de725f | 138 | tx[0]=HMC5843_Z_MSB; |
shimniok | 0:a6a169de725f | 139 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 140 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 141 | readings[2]= (short) (rx[0]<<8|rx[1]); |
shimniok | 0:a6a169de725f | 142 | |
shimniok | 0:a6a169de725f | 143 | return; |
shimniok | 0:a6a169de725f | 144 | } |
shimniok | 0:a6a169de725f | 145 | |
shimniok | 0:a6a169de725f | 146 | int HMC5843::getMx() |
shimniok | 0:a6a169de725f | 147 | { |
shimniok | 0:a6a169de725f | 148 | char tx[1]; |
shimniok | 0:a6a169de725f | 149 | char rx[2]; |
shimniok | 0:a6a169de725f | 150 | |
shimniok | 0:a6a169de725f | 151 | |
shimniok | 0:a6a169de725f | 152 | tx[0]=HMC5843_X_MSB; |
shimniok | 0:a6a169de725f | 153 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 154 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 155 | |
shimniok | 0:a6a169de725f | 156 | return (short) rx[0]<<8|rx[1]; |
shimniok | 0:a6a169de725f | 157 | } |
shimniok | 0:a6a169de725f | 158 | |
shimniok | 0:a6a169de725f | 159 | int HMC5843::getMy() |
shimniok | 0:a6a169de725f | 160 | { |
shimniok | 0:a6a169de725f | 161 | char tx[1]; |
shimniok | 0:a6a169de725f | 162 | char rx[2]; |
shimniok | 0:a6a169de725f | 163 | |
shimniok | 0:a6a169de725f | 164 | |
shimniok | 0:a6a169de725f | 165 | tx[0]=HMC5843_Y_MSB; |
shimniok | 0:a6a169de725f | 166 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 167 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 168 | |
shimniok | 0:a6a169de725f | 169 | return (short) rx[0]<<8|rx[1]; |
shimniok | 0:a6a169de725f | 170 | } |
shimniok | 0:a6a169de725f | 171 | |
shimniok | 0:a6a169de725f | 172 | |
shimniok | 0:a6a169de725f | 173 | int HMC5843::getMz(){ |
shimniok | 0:a6a169de725f | 174 | |
shimniok | 0:a6a169de725f | 175 | char tx[1]; |
shimniok | 0:a6a169de725f | 176 | char rx[2]; |
shimniok | 0:a6a169de725f | 177 | |
shimniok | 0:a6a169de725f | 178 | |
shimniok | 0:a6a169de725f | 179 | tx[0]=HMC5843_Z_MSB; |
shimniok | 0:a6a169de725f | 180 | i2c_->write(HMC5843_I2C_READ,tx,1); |
shimniok | 0:a6a169de725f | 181 | i2c_->read(HMC5843_I2C_READ,rx,2); |
shimniok | 0:a6a169de725f | 182 | |
shimniok | 0:a6a169de725f | 183 | return (short) rx[0]<<8|rx[1]; |
shimniok | 0:a6a169de725f | 184 | } |
shimniok | 0:a6a169de725f | 185 | |
shimniok | 0:a6a169de725f | 186 | |
shimniok | 0:a6a169de725f | 187 | int HMC5843::getStatus(void) |
shimniok | 0:a6a169de725f | 188 | { |
shimniok | 0:a6a169de725f | 189 | return 0; |
shimniok | 0:a6a169de725f | 190 | } |
shimniok | 0:a6a169de725f | 191 | |
shimniok | 0:a6a169de725f | 192 | |
shimniok | 0:a6a169de725f | 193 | void HMC5843::getAddress(char *buffer) |
shimniok | 0:a6a169de725f | 194 | { |
shimniok | 0:a6a169de725f | 195 | char rx[3]; |
shimniok | 0:a6a169de725f | 196 | char tx[1]; |
shimniok | 0:a6a169de725f | 197 | tx[0]=HMC5843_IDENT_A; |
shimniok | 0:a6a169de725f | 198 | |
shimniok | 0:a6a169de725f | 199 | |
shimniok | 0:a6a169de725f | 200 | i2c_->write(HMC5843_I2C_WRITE, tx,1); |
shimniok | 0:a6a169de725f | 201 | |
shimniok | 0:a6a169de725f | 202 | wait_ms(1); |
shimniok | 0:a6a169de725f | 203 | |
shimniok | 0:a6a169de725f | 204 | i2c_->read(HMC5843_I2C_READ,rx,3); |
shimniok | 0:a6a169de725f | 205 | |
shimniok | 0:a6a169de725f | 206 | buffer[0]=rx[0]; |
shimniok | 0:a6a169de725f | 207 | buffer[1]=rx[1]; |
shimniok | 0:a6a169de725f | 208 | buffer[2]=rx[2]; |
shimniok | 0:a6a169de725f | 209 | |
shimniok | 0:a6a169de725f | 210 | return; |
shimniok | 0:a6a169de725f | 211 | } |
shimniok | 0:a6a169de725f | 212 | |
shimniok | 0:a6a169de725f | 213 | |
shimniok | 0:a6a169de725f | 214 | void HMC5843::frequency(int hz) |
shimniok | 0:a6a169de725f | 215 | { |
shimniok | 0:a6a169de725f | 216 | if (hz == 100000) |
shimniok | 0:a6a169de725f | 217 | i2c_->frequency(100000); |
shimniok | 0:a6a169de725f | 218 | else if (hz == 400000) |
shimniok | 0:a6a169de725f | 219 | i2c_->frequency(400000); |
shimniok | 0:a6a169de725f | 220 | |
shimniok | 0:a6a169de725f | 221 | return; |
shimniok | 0:a6a169de725f | 222 | } |