mirror of
https://github.com/overte-org/overte.git
synced 2025-04-14 07:47:30 +02:00
read from magnetometer on SDA, SLC
This commit is contained in:
parent
15d74111d9
commit
a9050e6dc5
1 changed files with 44 additions and 6 deletions
|
@ -5,6 +5,8 @@
|
||||||
//
|
//
|
||||||
// PIN WIRING: Connect input sensors to the channels in following manner
|
// PIN WIRING: Connect input sensors to the channels in following manner
|
||||||
//
|
//
|
||||||
|
// SDA: Magnetomer SDA
|
||||||
|
// SDL: Magnetomer SDL
|
||||||
// AIN 10: Yaw Gyro (shaking your head 'no')
|
// AIN 10: Yaw Gyro (shaking your head 'no')
|
||||||
// AIN 16: Pitch Gyro (nodding your head 'yes')
|
// AIN 16: Pitch Gyro (nodding your head 'yes')
|
||||||
// AIN 17: Roll Gyro (looking quizzical, tilting your head)
|
// AIN 17: Roll Gyro (looking quizzical, tilting your head)
|
||||||
|
@ -12,11 +14,16 @@
|
||||||
// AIN 19: Up/Down acceleration (sitting up/ducking in front of your monitor)
|
// AIN 19: Up/Down acceleration (sitting up/ducking in front of your monitor)
|
||||||
// AIN 20: Forward/Back acceleration (Toward or away from your monitor)
|
// AIN 20: Forward/Back acceleration (Toward or away from your monitor)
|
||||||
|
|
||||||
|
// include HardWire for I2C communication to Magnetomer
|
||||||
|
#include <HardWire.h>
|
||||||
|
|
||||||
#define NUM_CHANNELS 6
|
#define NUM_CHANNELS 6
|
||||||
#define MSECS_PER_SAMPLE 10
|
#define MSECS_PER_SAMPLE 10
|
||||||
|
|
||||||
#define LED_PIN 12
|
#define LED_PIN 12
|
||||||
|
|
||||||
|
const int COMPASS_ADDRESS = 0x42 >> 1;
|
||||||
|
|
||||||
int inputPins[NUM_CHANNELS] = {10,16,17,18,19,20};
|
int inputPins[NUM_CHANNELS] = {10,16,17,18,19,20};
|
||||||
|
|
||||||
int LED = 0;
|
int LED = 0;
|
||||||
|
@ -26,8 +33,12 @@ unsigned int time;
|
||||||
int measured[NUM_CHANNELS];
|
int measured[NUM_CHANNELS];
|
||||||
float accumulate[NUM_CHANNELS];
|
float accumulate[NUM_CHANNELS];
|
||||||
|
|
||||||
|
bool readFromMag = false;
|
||||||
|
|
||||||
int sampleCount = 0;
|
int sampleCount = 0;
|
||||||
|
|
||||||
|
HardWire Wire(1, I2C_FAST_MODE);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -36,6 +47,7 @@ void setup()
|
||||||
measured[i] = analogRead(inputPins[i]);
|
measured[i] = analogRead(inputPins[i]);
|
||||||
accumulate[i] = measured[i];
|
accumulate[i] = measured[i];
|
||||||
}
|
}
|
||||||
|
Wire.begin();
|
||||||
pinMode(BOARD_LED_PIN, OUTPUT);
|
pinMode(BOARD_LED_PIN, OUTPUT);
|
||||||
pinMode(LED_PIN,OUTPUT);
|
pinMode(LED_PIN,OUTPUT);
|
||||||
time = millis();
|
time = millis();
|
||||||
|
@ -44,11 +56,22 @@ void setup()
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int compassReading;
|
||||||
|
bool magTransmit = false;
|
||||||
sampleCount++;
|
sampleCount++;
|
||||||
|
|
||||||
for (i = 0; i < NUM_CHANNELS; i++) {
|
for (i = 0; i < NUM_CHANNELS; i++) {
|
||||||
accumulate[i] += analogRead(inputPins[i]);
|
accumulate[i] += analogRead(inputPins[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (readFromMag && magTransmit == false) {
|
||||||
|
// get current absolute magnetometer value
|
||||||
|
Wire.beginTransmission(COMPASS_ADDRESS);
|
||||||
|
Wire.send('A');
|
||||||
|
Wire.endTransmission();
|
||||||
|
magTransmit = true;
|
||||||
|
}
|
||||||
|
|
||||||
if ((millis() - time) >= MSECS_PER_SAMPLE) {
|
if ((millis() - time) >= MSECS_PER_SAMPLE) {
|
||||||
samplesSent++;
|
samplesSent++;
|
||||||
time = millis();
|
time = millis();
|
||||||
|
@ -58,7 +81,7 @@ void loop()
|
||||||
SerialUSB.print(" ");
|
SerialUSB.print(" ");
|
||||||
accumulate[i] = 0;
|
accumulate[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((samplesSent % 100 == 0) && (samplesSent % 150 == 0)) {
|
if ((samplesSent % 100 == 0) && (samplesSent % 150 == 0)) {
|
||||||
LED = !LED;
|
LED = !LED;
|
||||||
digitalWrite(LED_PIN, LED);
|
digitalWrite(LED_PIN, LED);
|
||||||
|
@ -72,10 +95,25 @@ void loop()
|
||||||
else
|
else
|
||||||
SerialUSB.print("0");
|
SerialUSB.print("0");
|
||||||
|
|
||||||
SerialUSB.println("");
|
SerialUSB.print(" ");
|
||||||
sampleCount = 0;
|
sampleCount = 0;
|
||||||
|
|
||||||
|
if (readFromMag) {
|
||||||
|
// send the absolute value from the magnetomer
|
||||||
|
Wire.requestFrom(COMPASS_ADDRESS, 2);
|
||||||
|
|
||||||
|
if (2 <= Wire.available()) {
|
||||||
|
compassReading = Wire.receive();
|
||||||
|
compassReading = compassReading << 8;
|
||||||
|
compassReading += Wire.receive();
|
||||||
|
compassReading /= 10;
|
||||||
|
SerialUSB.print(compassReading);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SerialUSB.println("");
|
||||||
|
|
||||||
|
// reset the magnetomerTransmit to false to prep it in next loop
|
||||||
|
magTransmit = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue