Hey, i'm pretty new to coding and Arduino, but I understand the basics.
My project is basically trying to create a stabilization system for a single axis using 9 axis DOF gyro sensor and a servo motor.
I have managed to get signal out the shield for just the X axis, however I do not think this is right as I want the angle not meters per second.
To begin, ideally I want the code to output the x-axis angle from the gyro to the servo motor, from there I can figure it out.
The code I have at the moment is loosely based of a few codes I found on the Arduino site:
#include "NAxisMotion.h" //Contains the bridge code between the API and the Arduino Environment
#include <Wire.h>
#include <Servo.h>
NAxisMotion mySensor; //Object that for the sensor
unsigned long lastStreamTime = 0; //To store the last streamed time stamp
const int streamPeriod = 40; //To stream at 25Hz without using additional timers (time period(ms) =1000/frequency(Hz))
bool updateSensorData = true; //Flag to update the sensor data. Default is true to perform the first read before the first stream
Servo myservo; // create servo object to control a servo
#define pinServo A0
int speed = 0;
int limits[2] = {0,180}; // set limitations (min/max: 0->180)
boolean refresh = false; // toggle refresh on/off
void setup() //This code is executed once
{
//Peripheral Initialization
Serial.begin(115200); //Initialize the Serial Port to view information on the Serial Monitor
// attaches the servo on pin to the servo object
myservo.attach(pinServo);
// init angle of servo inbetween two limitations
myservo.write((limits[1]-limits[0])/2);
I2C.begin(); //Initialize I2C communication to the let the library communicate with the sensor.
//Sensor Initialization
mySensor.initSensor(); //The I2C Address can be changed here inside this function in the library
mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
//Setting to MANUAL requires lesser reads to the sensor
mySensor.updateAccelConfig();
updateSensorData = true;
Serial.println();
Serial.println("Default accelerometer configuration settings...");
Serial.print("Range: ");
Serial.println(mySensor.readAccelRange());
Serial.print("Bandwidth: ");
Serial.println(mySensor.readAccelBandwidth());
Serial.print("Power Mode: ");
Serial.println(mySensor.readAccelPowerMode());
Serial.println("Streaming in ..."); //Countdown
Serial.print("3...");
delay(1000); //Wait for a second
Serial.print("2...");
delay(1000); //Wait for a second
Serial.println("1...");
delay(1000); //Wait for a second
}
void loop() //This code is looped forever
{
if (updateSensorData) //Keep the updating of data as a separate task
{
mySensor.updateGyro(); //Update the Gyro data
updateSensorData = false;
}
if ((millis() - lastStreamTime) >= streamPeriod)
{
lastStreamTime = millis();
Serial.print("Time: ");
Serial.print(lastStreamTime);
Serial.print("ms ");
Serial.print(" rX: ");
Serial.print(mySensor.readGyroX());
Serial.print("m/s2 ");
Serial.print(" C: ");
Serial.print(mySensor.readAccelCalibStatus()); //Accelerometer Calibration Status (0 - 3)
Serial.println();
updateSensorData = true;
int angle = myservo.read();
// change direction when limits
if (angle >= limits[1] || angle <= limits[0]) speed = -speed;
myservo.write(angle + speed);
Serial.print("Angle: ");
Serial.println(angle);
delay(1000);
}
}
The code is a mess at the moment and I understand parts of it, if anyone can point me in the right direction (Keep in mind I've never coded in my life) or help me sort my code out, much would be appreciated. Thank you in advance!