Hey, I'm working on an AHRS project where a Razor 9DOF is used to sense changes in direction of an AUV.
My problem is that when using the razor to output euler angles, such as the code on the sparkfun website does, the yaw is referenced so that it's zero position is north. Our current code uses the euler angles to actuate itself around the zero positions, which is fine for pitch and roll, but in most cases we want the yaw to actuate around its initial starting value.
My question is does anyone know of a way of reading the initial starting value of yaw, then setting that to the reference value which the rotation in yaw is calculated around.
#include <NewSoftSerial.h>
NewSoftSerial myserial = NewSoftSerial(17, 16);
// define motor driver pin numbers:
const int lefthorzA = 22;
const int lefthorzB = 23;
const int righthorzA = 24;
const int righthorzB = 25;
const int leftvertA = 26;
const int leftvertB = 27;
const int rightvertA = 28;
const int rightvertB = 29;
const int frontvertA = 30;
const int frontvertB = 31;
const int rearvertA = 32;
const int rearvertB = 33;
// define switch position pin numbers:
const int switchposA = 51;
const int switchposB = 52;
const int switchposC = 53;
delay (5000);
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Myserial.begin(9600);
// set the digital pins as inputs or outputs:
pinMode(lefthorzA, OUTPUT);
pinMode(lefthorzB, OUTPUT);
pinMode(righthorzA, OUTPUT);
pinMode(righthorzB, OUTPUT);
pinMode(leftvertA, OUTPUT);
pinMode(leftvertB, OUTPUT);
pinMode(rightvertA, OUTPUT);
pinMode(rightvertB, OUTPUT);
pinMode(frontvertA, OUTPUT);
pinMode(frontvertB, OUTPUT);
pinMode(rearvertA, OUTPUT);
pinMode(rearvertB, OUTPUT);
pinMode(switchposA, INPUT);
pinMode(switchposB, INPUT);
pinMode(switchposC, INPUT);
}
bool ReadingString = false; // Are we building a string?
char Buffer[17]; // Build up the string from the Razor here
byte BufferIndex = 0; // Next free position in the buffer
int roll = 0; // Value from the Razor
int pitch = 0; // Value from the Razor
int yaw = 0; // Value from the Razor
int valueA = 0;
int valueA = 0;
int valueA = 0;
void loop() {
// read the output of the switch:
valueA = digitalRead(switchposA); // read the input from switch position pin A
valueB = digitalRead(switchposB); // read the input from switch position pin B
valueC = digitalRead(switchposC); // read the input from switch position pin C
if (valueA = HIGH) {
delay(180000);
unsigned long time;
time = millis();
if (time<120000) {
int c;
if(Serial1.available()) // Something there?
{
c=Serial1.read(); // Get a character
if(c=='!') // Use the ! to indicate the start of a series to read
{
ReadingString=true; // Now we're reading to the buffer, rather than just
// throwing stuff away
BufferIndex=0; // Reset to the start of the buffer
}
else //Not a !. If we're mid string, store it, otherwise, throw it away
{
if(ReadingString) //Should we store it?
Buffer[BufferIndex++]=c;
if(BufferIndex>14) // Have we read an entire Razor string
{
ReadingString=false; // Done reading
Buffer[BufferIndex]=0; // Set a terminator for the last atoi
BufferIndex=0; // Reset to start of buffer for next string
// Read & display the values we got
roll = atoi(&Buffer[4]);
pitch = atoi(&Buffer[8]);
yaw = atoi(&Buffer[12]);
Serial.print("Angles ");
Serial.print (roll);
Serial.print(" ");
Serial.print (pitch);
Serial.print(" ");
Serial.print (yaw);
Serial.println(" ");
mySerial.print("Angles ");
mySerial.print (roll);
mySerial.print(" ");
mySerial.print (pitch);
mySerial.print(" ");
mySerial.print (yaw);
mySerial.println(" ");
}
}
}
}
}
if (180<roll<=355) {
// roll craft left:
digitalWrite (leftvertA, HIGH);
digitalWrite (leftvertB, LOW);
digitalWrite (rightvertA, HIGH);
digitalWrite (rightvertB, LOW);
}
if (365>=roll<540) {
// roll craft right:
digitalWrite (leftvertA, LOW);
digitalWrite (leftvertB, HIGH);
digitalWrite (rightvertA, LOW);
digitalWrite (rightvertB, HIGH);
}
if (180<pitch<=355) {
// tilt nose down:
digitalWrite (frontvertA, HIGH);
digitalWrite (frontvertB, LOW);
digitalWrite (rearvertA, HIGH);
digitalWrite (rearvertB, LOW);
}
if (365>=pitch<540) {
// tilt nose up:
digitalWrite (frontvertA, LOW);
digitalWrite (frontvertB, HIGH);
digitalWrite (rearvertA, LOW);
digitalWrite (rearvertB, HIGH);
}
if (180<yaw<=355) {
// rotate craft clockwise:
digitalWrite (lefthorzA, LOW);
digitalWrite (lefthorzB, HIGH);
digitalWrite (righthorzA, LOW);
digitalWrite (righthorzB, HIGH);;
}
if (365>=yaw<540) {
// rotate craft anticlockwise:
digitalWrite (lefthorzA, HIGH);
digitalWrite (lefthorzB, LOW);
digitalWrite (righthorzA, HIGH);
digitalWrite (righthorzB, LOW);
}
else {
// turn thrusters off:
digitalWrite (lefthorzA, LOW);
digitalWrite (lefthorzB, LOW);
digitalWrite (righthorzA, LOW);
digitalWrite (righthorzB, LOW);
digitalWrite (leftvertA, LOW);
digitalWrite (leftvertB, LOW);
digitalWrite (rightvertA, LOW);
digitalWrite (rightvertB, LOW);
digitalWrite (frontvertA, LOW);
digitalWrite (frontvertB, LOW);
digitalWrite (rearvertA, LOW);
digitalWrite (rearvertB, LOW);
}
}
else if (valueB = HIGH){
delay(180000);
unsigned long time;
time = millis();
int c;
if(Serial1.available()) // Something there?
{
c=Serial1.read(); // Get a character
if(c=='!') // Use the ! to indicate the start of a series to read
{
ReadingString=true; // Now we're reading to the buffer, rather than just throwing stuff away
BufferIndex=0; // Reset to the start of the buffer
}
else //Not a !. If we're mid string, store it, otherwise, throw it away
{
if(ReadingString) //Should we store it?
Buffer[BufferIndex++]=c;
if(BufferIndex>14) // Have we read an entire Razor string
{
ReadingString=false; // Done reading
Buffer[BufferIndex]=0; // Set a terminator for the last atoi
BufferIndex=0; // Reset to start of buffer for next string
// Read & display the values we got
roll=atoi(&Buffer[4]);
pitch=atoi(&Buffer[8]);
yaw=atoi(&Buffer[12]);
Serial.print("Angles ");
Serial.print (roll);
Serial.print(" ");
Serial.print (pitch);
Serial.print(" ");
Serial.print (yaw);
Serial.println(" ");
mySerial.print("Angles ");
mySerial.print (roll);
mySerial.print(" ");
mySerial.print (pitch);
mySerial.print(" ");
mySerial.print (yaw);
mySerial.println(" ");
}
}
}
if (time<60000) {
// fire horizontal thrusters forwards for 20s:
digitalWrite (lefthorzA, HIGH);
digitalWrite (lefthorzB, LOW);
digitalWrite (righthorzA, LOW);
digitalWrite (righthorzB, HIGH);
}
else if (60000>=time<120000 ){
// fire horizontal thrusters backwards for 20s:
digitalWrite (lefthorzA, LOW);
digitalWrite (lefthorzB, HIGH);
digitalWrite (righthorzA, HIGH);
digitalWrite (righthorzB, LOW);
}
else {
// turn thrusters off:
digitalWrite (lefthorzA, LOW);
digitalWrite (lefthorzB, LOW);
digitalWrite (righthorzA, LOW);
digitalWrite (righthorzB, LOW);
}
if (time<120000) {
}
if (180<roll<=355) {
// roll craft left:
digitalWrite (leftvertA, HIGH);
digitalWrite (leftvertB, LOW);
digitalWrite (rightvertA, HIGH);
digitalWrite (rightvertB, LOW);
}
if (365>=roll<540) {
// roll craft right:
digitalWrite (leftvertA, LOW);
digitalWrite (leftvertB, HIGH);
digitalWrite (rightvertA, LOW);
digitalWrite (rightvertB, HIGH);
}
if (180<pitch<=355) {
// tilt nose down:
digitalWrite (frontvertA, HIGH);
digitalWrite (frontvertB, LOW);
digitalWrite (rearvertA, HIGH);
digitalWrite (rearvertB, LOW);
}
if (365>=pitch<540) {
// tilt nose up:
digitalWrite (frontvertA, LOW);
digitalWrite (frontvertB, HIGH);
digitalWrite (rearvertA, LOW);
digitalWrite (rearvertB, HIGH);
}
else {
// turn thrusters off:
digitalWrite (leftvertA, LOW);
digitalWrite (leftvertB, LOW);
digitalWrite (rightvertA, LOW);
digitalWrite (rightvertB, LOW);
digitalWrite (frontvertA, LOW);
digitalWrite (frontvertB, LOW);
digitalWrite (rearvertA, LOW);
digitalWrite (rearvertB, LOW);
}
}
else {
; // do nothing
}
}