|
| 1 | +#include "Error.h" |
| 2 | +#include "IMU.h" |
| 3 | +#include "LSM6DS_LIS3MDL.h" |
| 4 | + |
| 5 | +/* |
| 6 | +IMU.cpp |
| 7 | +Description: Function definititions for declarations in IMU.h |
| 8 | +Author: Vincent Palmerio |
| 9 | +
|
| 10 | +*/ |
| 11 | + |
| 12 | + |
| 13 | +Eigen::VectorXd linearAccelVector(3); |
| 14 | +float linearAccelX, linearAccelY, linearAccelZ = 0; |
| 15 | +Eigen::VectorXd gyroscopeVector(3); |
| 16 | +Eigen::VectorXd magnetometerVector(3); |
| 17 | +float roll, pitch, yaw = 0; |
| 18 | +float gx, gy, gz = 0; //degrees per second on gyro |
| 19 | +float qw, qx, qy, qz = 0; //quaternarion |
| 20 | + |
| 21 | +/* |
| 22 | + * You must free the pointer and set it to NULL after using the pointer! |
| 23 | + */ |
| 24 | +float* getValues() { |
| 25 | + float* values = (float*)malloc(3 * sizeof(float)); |
| 26 | + values[0] = roll; |
| 27 | + values[1] = pitch; |
| 28 | + values[2] = yaw; |
| 29 | + return values; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +//loads a predetermined calibration into the EEPROM |
| 34 | +int loadPresetCalibration() { |
| 35 | + //Magnetic Hard Offset |
| 36 | + cal.mag_hardiron[0] = -3.35; |
| 37 | + cal.mag_hardiron[1] = -0.74; |
| 38 | + cal.mag_hardiron[2] = -40.79; |
| 39 | + |
| 40 | + //Magnetic Soft Offset |
| 41 | + // in uTesla |
| 42 | + cal.mag_softiron[0] = 0.96; |
| 43 | + cal.mag_softiron[1] = 0.02; |
| 44 | + cal.mag_softiron[2] = 0.01; |
| 45 | + cal.mag_softiron[3] = 0.02; |
| 46 | + cal.mag_softiron[4] = 0.96; |
| 47 | + cal.mag_softiron[5] = 0.0; |
| 48 | + cal.mag_softiron[6] = 0.01; |
| 49 | + cal.mag_softiron[7] = 0.0; |
| 50 | + cal.mag_softiron[8] = 1.08; |
| 51 | + |
| 52 | + //Gyro zero rate offset |
| 53 | + // in Radians/s |
| 54 | + cal.gyro_zerorate[0] = 0.05; |
| 55 | + cal.gyro_zerorate[1] = -0.01; |
| 56 | + cal.gyro_zerorate[2] = -0.01; |
| 57 | + |
| 58 | + if (cal.saveCalibration()) { |
| 59 | + return FAILED_LOAD_CALIBRATION; |
| 60 | + } |
| 61 | + |
| 62 | + return NO_ERROR_CODE; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +int initializeIMU() { |
| 68 | + //Serial.begin(115200); |
| 69 | + //while (!Serial) yield(); |
| 70 | + //Serial.println("test"); |
| 71 | + for (int i = 0; i < linearAccelVector.size(); i++) { |
| 72 | + linearAccelVector(i) = 0; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + if (!cal.begin()) { |
| 77 | + //Failed to initialize calibration helper |
| 78 | +#if defined(ASTRA_FULL_DEBUG) or defined(ASTRA_IMU_DEBUG) |
| 79 | + Serial.print("Failed to initialize calibration helper"); |
| 80 | +#endif |
| 81 | + return FAILED_CALIBRATION_HELPER_INIT; |
| 82 | + } |
| 83 | + |
| 84 | + if (!cal.loadCalibration()) { |
| 85 | + //No calibration loaded/found |
| 86 | + static bool triedLoadPresetCalibration = false; |
| 87 | + if (!triedLoadPresetCalibration) { |
| 88 | + if (loadPresetCalibration() == FAILED_LOAD_CALIBRATION) { |
| 89 | + return FAILED_LOAD_CALIBRATION; |
| 90 | + } |
| 91 | + } |
| 92 | +#if defined(ASTRA_FULL_DEBUG) or defined(ASTRA_IMU_DEBUG) |
| 93 | + Serial.println("Failed to load calibration"); |
| 94 | +#endif |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + if (!init_sensors(&IMU_WIRE)) { |
| 99 | + //Failed to find sensors |
| 100 | +#if defined(ASTRA_FULL_DEBUG) or defined(ASTRA_IMU_DEBUG) |
| 101 | + Serial.println("Failed to find sensors"); |
| 102 | +#endif |
| 103 | + return FAILED_SENSOR_INIT; |
| 104 | + } |
| 105 | + |
| 106 | +#if defined(ASTRA_FULL_DEBUG) or defined(ASTRA_IMU_DEBUG) |
| 107 | + accelerometer->printSensorDetails(); |
| 108 | + gyroscope->printSensorDetails(); |
| 109 | + magnetometer->printSensorDetails(); |
| 110 | +#endif |
| 111 | + |
| 112 | + setup_sensors(); |
| 113 | + filter.begin(FILTER_UPDATE_RATE_HZ); |
| 114 | + |
| 115 | + Wire.setClock(400000); // 400KHz |
| 116 | + |
| 117 | + return NO_ERROR_CODE; |
| 118 | +} |
| 119 | + |
| 120 | +//sets varaibles declared in IMU.h based on latest data IMU |
| 121 | +int updateIMU() { |
| 122 | + |
| 123 | + // Read the motion sensors |
| 124 | + sensors_event_t accel, gyro, mag; |
| 125 | + accelerometer->getEvent(&accel); |
| 126 | + gyroscope->getEvent(&gyro); |
| 127 | + magnetometer->getEvent(&mag); |
| 128 | + |
| 129 | + cal.calibrate(mag); |
| 130 | + cal.calibrate(accel); |
| 131 | + cal.calibrate(gyro); |
| 132 | + |
| 133 | + // Gyroscope needs to be converted from Rad/s to Degree/s |
| 134 | + // the rest are not unit-important |
| 135 | + |
| 136 | + gx = gyro.gyro.x; //* SENSORS_RADS_TO_DPS; //omega x |
| 137 | + gy = gyro.gyro.y; //* SENSORS_RADS_TO_DPS; //omega y |
| 138 | + gz = gyro.gyro.z; //* SENSORS_RADS_TO_DPS; //omega z |
| 139 | + |
| 140 | + // Update the SensorFusion filter |
| 141 | + filter.update(gx, gy, gz, |
| 142 | + accel.acceleration.x, accel.acceleration.y, accel.acceleration.z, |
| 143 | + mag.magnetic.x, mag.magnetic.y, mag.magnetic.z); |
| 144 | + |
| 145 | + // print the heading, pitch and roll |
| 146 | + roll = filter.getRoll(); |
| 147 | + pitch = filter.getPitch(); |
| 148 | + yaw = filter.getYaw(); |
| 149 | + |
| 150 | + |
| 151 | + //float qw, qx, qy, qz; |
| 152 | + filter.getQuaternion(&qw, &qx, &qy, &qz); |
| 153 | + |
| 154 | + |
| 155 | + filter.getLinearAcceleration(&linearAccelX, &linearAccelY, &linearAccelZ); //"a" - linear acceleration |
| 156 | + |
| 157 | + linearAccelVector << linearAccelX, linearAccelY, linearAccelZ; |
| 158 | + gyroscopeVector << gx, gy, gz; |
| 159 | + magnetometerVector << mag.magnetic.x, mag.magnetic.y, mag.magnetic.z; |
| 160 | + |
| 161 | +#if defined(ASTRA_FULL_DEBUG) or defined(ASTRA_IMU_DEBUG) |
| 162 | + |
| 163 | + //Serial.print("I2C took "); Serial.print(millis()-timestamp); Serial.println(" ms"); |
| 164 | + |
| 165 | + //Serial.print("Update took "); Serial.print(millis()-timestamp); Serial.println(" ms"); |
| 166 | + Serial.print("Raw: "); |
| 167 | + Serial.print(accel.acceleration.x, 4); Serial.print(", "); |
| 168 | + Serial.print(accel.acceleration.y, 4); Serial.print(", "); |
| 169 | + Serial.print(accel.acceleration.z, 4); Serial.print(", "); |
| 170 | + Serial.print(gx, 4); Serial.print(", "); |
| 171 | + Serial.print(gy, 4); Serial.print(", "); |
| 172 | + Serial.print(gz, 4); Serial.print(", "); |
| 173 | + Serial.print(mag.magnetic.x, 4); Serial.print(", "); |
| 174 | + Serial.print(mag.magnetic.y, 4); Serial.print(", "); |
| 175 | + Serial.print(mag.magnetic.z, 4); Serial.println(""); |
| 176 | + |
| 177 | + Serial.print("Orientation: "); |
| 178 | + Serial.print(heading); |
| 179 | + Serial.print(", "); |
| 180 | + Serial.print(pitch); |
| 181 | + Serial.print(", "); |
| 182 | + Serial.println(roll); |
| 183 | + |
| 184 | + Serial.print("Quaternion: "); |
| 185 | + Serial.print(qw, 4); |
| 186 | + Serial.print(", "); |
| 187 | + Serial.print(qx, 4); |
| 188 | + Serial.print(", "); |
| 189 | + Serial.print(qy, 4); |
| 190 | + Serial.print(", "); |
| 191 | + Serial.println(qz, 4); |
| 192 | + //Serial.print("Took "); Serial.print(millis()-timestamp); Serial.println(" ms"); |
| 193 | + |
| 194 | +#endif |
| 195 | + |
| 196 | + |
| 197 | + return NO_ERROR_CODE; |
| 198 | + |
| 199 | +} |
| 200 | + |
0 commit comments