Updated imu.h and imu.ino and increased command argv array size from 16 to 32 to handel PID update

This commit is contained in:
Patrick Goebel 2016-04-11 08:03:37 -07:00
parent 6234b9260a
commit 79e5a3b1a8
3 changed files with 31 additions and 16 deletions

View File

@ -143,6 +143,7 @@
#ifdef USE_IMU #ifdef USE_IMU
#include "imu.h" #include "imu.h"
// The only IMU currently supported is the Adafruit 9-DOF IMU
#define ADAFRUIT_9DOF #define ADAFRUIT_9DOF
#endif #endif
@ -161,8 +162,8 @@ char chr;
char cmd; char cmd;
// Character arrays to hold the first and second arguments // Character arrays to hold the first and second arguments
char argv1[16]; char argv1[32];
char argv2[16]; char argv2[32];
// The arguments converted to integers // The arguments converted to integers
long arg1; long arg1;
@ -220,6 +221,9 @@ int runCommand() {
#ifdef USE_IMU #ifdef USE_IMU
case READ_IMU: case READ_IMU:
imu_data = readIMU(); imu_data = readIMU();
/* Send the IMU data base in the following order
* [ax, ay, az, gx, gy, gz, mx, my, mz, roll, pitch, ch]
*/
Serial.print(imu_data.ax); Serial.print(imu_data.ax);
Serial.print(F(" ")); Serial.print(F(" "));
Serial.print(imu_data.ay); Serial.print(imu_data.ay);
@ -242,7 +246,7 @@ int runCommand() {
Serial.print(F(" ")); Serial.print(F(" "));
Serial.print(imu_data.pitch); Serial.print(imu_data.pitch);
Serial.print(F(" ")); Serial.print(F(" "));
Serial.println(imu_data.uh); Serial.println(imu_data.ch);
break; break;
#endif #endif
#ifdef USE_SERVOS #ifdef USE_SERVOS
@ -322,8 +326,8 @@ int runCommand() {
case UPDATE_PID: case UPDATE_PID:
i = 0; i = 0;
while ((str = strtok_r(p, ":", &p)) != '\0') { while ((str = strtok_r(p, ":", &p)) != '\0') {
pid_args[i] = atoi(str); pid_args[i] = atoi(str);
i++; i++;
} }
Kp = pid_args[0]; Kp = pid_args[0];
Kd = pid_args[1]; Kd = pid_args[1];

View File

@ -3,6 +3,16 @@
#ifndef IMU_H #ifndef IMU_H
#define IMU_H #define IMU_H
/*
IMU data is assumed to be returned in the following order:
[ax, ay, az, gx, gy, gz, mx, my, mz, roll, pitch, ch]
where a stands for accelerometer, g for gyroscope and m for magnetometer.
The last value ch stands for "compensated heading" that some IMU's can
compute to compensate magnetic heading from the current roll and pitch.
*/
typedef struct imuData_s typedef struct imuData_s
{ {
float ax = -999; float ax = -999;
@ -16,7 +26,7 @@ typedef struct imuData_s
float mz = -999; float mz = -999;
float roll = -999; float roll = -999;
float pitch = -999; float pitch = -999;
float uh = -999; float ch = -999;
} imuData; } imuData;
void initIMU(); void initIMU();

View File

@ -12,22 +12,17 @@
Adafruit_9DOF dof = Adafruit_9DOF(); Adafruit_9DOF dof = Adafruit_9DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301); Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302); Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);
/* Update this with the correct SLP for accurate altitude measurements */ /* Update this with the correct SLP for accurate altitude measurements */
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
void initIMU() void initIMU()
{ {
if(!accel.begin()) accel.begin();
{ mag.begin();
/* There was a problem detecting the LSM303 ... check your connections */ gyro.begin();
Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
}
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
}
} }
imuData readIMU() { imuData readIMU() {
@ -36,6 +31,7 @@
sensors_event_t accel_event; sensors_event_t accel_event;
sensors_event_t mag_event; sensors_event_t mag_event;
sensors_vec_t orientation; sensors_vec_t orientation;
sensors_event_t event;
/* Calculate pitch and roll from the raw accelerometer data */ /* Calculate pitch and roll from the raw accelerometer data */
accel.getEvent(&accel_event); accel.getEvent(&accel_event);
@ -43,6 +39,11 @@
data.ay = accel_event.acceleration.y; data.ay = accel_event.acceleration.y;
data.az = accel_event.acceleration.z; data.az = accel_event.acceleration.z;
gyro.getEvent(&event);
data.gx = event.gyro.x;
data.gy = event.gyro.y;
data.gz = event.gyro.z;
if (dof.accelGetOrientation(&accel_event, &orientation)) if (dof.accelGetOrientation(&accel_event, &orientation))
{ {
/* 'orientation' should have valid .roll and .pitch fields */ /* 'orientation' should have valid .roll and .pitch fields */