Help with HM55B Compass
as of right hm55b compass printing constant data processing -180 0n 180. how modify following code output 8 directions. n, ne, e, se, s, sw, w, nw?
code: [select]
#include <math.h> // (no semicolon)
//// vars
byte clk_pin = 8;
byte en_pin = 9;
byte dio_pin = 10;
byte led = 13;
int zerooffset = 0; // calibrate compass, run in uncalibrated mode [section 1],
// point sensor due north, , record value sensed.
// enter value here , run in calibrated mode [section 2]
int x_data = 0;
int y_data = 0;
int angle, status;
float delta, xprime, yprime;
//// functions
void shiftout(int value, int bitscount) {
for(int = bitscount; >= 0; i--) {
digitalwrite(clk_pin, low);
if ((value & 1 << i) == ( 1 << i)) {
digitalwrite(dio_pin, high);
//serial.print("1");
}
else {
digitalwrite(dio_pin, low);
//serial.print("0");
}
digitalwrite(clk_pin, high);
delaymicroseconds(1);
}
}
int shiftin(int bitscount) {
int shiftin_result;
shiftin_result = 0;
pinmode(dio_pin, input);
for(int = bitscount; >= 0; i--) {
digitalwrite(clk_pin, high);
delaymicroseconds(1);
if (digitalread(dio_pin) == high) {
shiftin_result = (shiftin_result << 1) + 1;
//serial.print("x");
}
else {
shiftin_result = (shiftin_result << 1) + 0;
//serial.print("_");
}
digitalwrite(clk_pin, low);
delaymicroseconds(1);
}
//serial.print(":");
// below difficult understand:
// if bit 11 set value negative
// representation of negative values you
// have add b11111000 in upper byte of
// integer.
// see: http://en.wikipedia.org/wiki/two%27s_complement
if ((shiftin_result & 1 << 11) == 1 << 11) {
shiftin_result = (b11111000 << 8) | shiftin_result;
}
return shiftin_result;
}
void hm55b_reset() {
pinmode(dio_pin, output);
digitalwrite(en_pin, low);
shiftout(b0000, 3);
digitalwrite(en_pin, high);
}
void hm55b_startmeasurementcommand() {
pinmode(dio_pin, output);
digitalwrite(en_pin, low);
shiftout(b1000, 3);
digitalwrite(en_pin, high);
}
int hm55b_readcommand() {
int result = 0;
pinmode(dio_pin, output);
digitalwrite(en_pin, low);
shiftout(b1100, 3);
result = shiftin(3);
return result;
}
void setup() {
serial.begin(9600);
pinmode(en_pin, output);
pinmode(clk_pin, output);
pinmode(dio_pin, input);
serial.println();
hm55b_reset();
}
void loop() {
hm55b_startmeasurementcommand(); // necessary!!
delay(40); // data 40ms later ready
status = hm55b_readcommand(); // read data
x_data = shiftin(11); // field strength in x
y_data = shiftin(11); // , y direction
digitalwrite(en_pin, high); // deselect chip
// [section 1: simple angle calculation]
// to determine angle using raw sensor data, can calculate
// the inverse tangent of y_data/x_data. uncomment following line , comment
// all of [section 2]
// angle = (180 * (atan2(-1 * y_data , x_data) / m_pi)); // angle atan( -y/x) !!!
// [end section 1]
// [section 2: offset-compensated angle calculation]
// to better match compass readings known values of true north,
// you can use following calculations rotate value of tangent
// before determining angle. to this, first need run program
// with code in [section 1] uncommented , note angle value when we
// point sensor true north. enter value recorded @ top of
// this program in variable "zerooffset", comment code in [section 1],
// and uncomment code below.
delta = (zerooffset/180.0)*m_pi; // calculate radians degrees
xprime = x_data*cos(delta) - y_data*sin(delta); // translate x_data value based on
// rotating original angle "delta" rads.
yprime = y_data*cos(delta) + x_data*sin(delta); // translate y_data value based on
// rotating original angle "delta" rads.
angle = (180 * (atan2(-1 * yprime , xprime) / m_pi)); // angle still atan(-y/x)
// [end section 2]
if (abs(angle) < 10) {
digitalwrite (led, high);
} else {
digitalwrite (led, low);
}
serial.println(angle); // print angle
delay(100);
}
assuming angle contains value between 0 , 359:
beware, though, haven't been able try it.
code: [select]
char* directions[]={"n", "ne", "e", "se", "s", "sw", "w", "nw"};
serial.println(directions[((angle+22)/45)%8]);beware, though, haven't been able try it.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Help with HM55B Compass
arduino
Comments
Post a Comment