4 // Hi-Technic Acceleration / Tilt sensor class
6 // Written 12-jan-2008 by rwk
8 // Copyright 2007, 2008 by Robert W. Kramer and Takashi Chikamasa
11 #include "AccelSensor.h"
13 //=============================================================================
14 // AccelSensor::AccelSensor(unsigned char _port)
15 // constructor for sensor object
18 // _port - port number sensor is connected to
21 AccelSensor::AccelSensor(unsigned char _port) {
24 // remember the port number for data transfers
30 // turn on the sensor port. This comes from Takashi's code
33 nxt_avr_set_input_power(port,POWER_ON);
37 // we are now activated (avoids redundant calls later)
45 //=============================================================================
46 // AccelSensor::~AccelSensor(void)
50 AccelSensor::~AccelSensor(void) {
53 // turn off power. Not sure if this is needed here.
56 nxt_avr_set_input_power(port,POWER_OFF);
59 // disable the port. This is necessary.
67 //=============================================================================
68 // void AccelSensor::activate(void)
69 // turn on the sensor port
72 void AccelSensor::activate(void) {
75 // nothing to do if the port's already on
82 // turn on the port and power
85 nxt_avr_set_input_power(port,POWER_ON);
89 // remember that we've done this
97 //=============================================================================
98 // void AccelSensor::passivate(void)
102 void AccelSensor::passivate(void) {
105 // nothing to do if the port's off
112 // turn off the port. As with the destructor, I'm not sure if the first call
116 nxt_avr_set_input_power(port,POWER_OFF);
120 // remember what we did
128 //=============================================================================
129 // void AccelSensor::fetchData(short int axes[3])
130 // fetch acceleration data from the sensor
133 // axes - array to hold acceleration data along the axes
136 void AccelSensor::fetchData(short int axes[3]) {
138 unsigned char inBuf[6];
141 // Data representation from the sensor, per Hi-Technic's documentation:
143 // data[0]: X axis upper 8 bits
144 // data[1]: Y axis upper 8 bits
145 // data[2]: Z axis upper 8 bits
146 // data[3]: X axis lower 2 bits
147 // data[4]: Y axis lower 2 bits
148 // data[5]: Z axis lower 2 bits
152 // wait for port to become available
159 // initiate data transfer
162 i2c_start_transaction(port,1,0x42,1,inBuf,6,0);
165 // wait for port to complete the transfer
172 // convert raw data into final results
175 for (i=0; i<3; i++) {
176 axes[i] = (S16)inBuf[i];
177 if (axes[i] > 127) axes[i] -= 256;
178 /* convert to 10 bit value */
179 axes[i] = (axes[i] << 2) | ((S16)inBuf[i+3] & 0x0003);
185 //=============================================================================
186 // short int AccelSensor::getXAccel(void)
187 // get acceleration on X axis
189 // NB: positive is forward. 200 ticks per g.
192 short int AccelSensor::getXAccel(void) {
202 //=============================================================================
203 // short int AccelSensor::getYAccel(void)
204 // get acceleration on Y axis
206 // NB: positive is left. 200 ticks per g.
209 short int AccelSensor::getYAccel(void) {
219 //=============================================================================
220 // short int AccelSensor::getZAccel(void)
221 // get acceleration on Z axis
223 // NB: positive is up. 200 ticks per g.
226 short int AccelSensor::getZAccel(void) {
236 //=============================================================================
237 // void AccelSensor::getAccel(short int axes[3])
238 // get acceleration on all three axes
241 void AccelSensor::getAccel(short int axes[3]) {
249 //=============================================================================
250 // void AccelSensor::getAccel(short int &x,short int &y,short int &z) {
251 // get acceleration on all three axes
254 void AccelSensor::getAccel(short int &x,short int &y,short int &z) {