diff --git a/controller/calibration.ino b/controller/calibration.ino index 7386153133e0573051358f93baae5b07454abdc4..db8f68368a9025173e6804ce4035bf03cc89ad3c 100644 --- a/controller/calibration.ino +++ b/controller/calibration.ino @@ -7,8 +7,11 @@ Then we determine the maximum position so we can make it match the maximum angle attainable. + WARNING: Not sure if this function is working properly or not. The second test + campaign (Q3 2023) did not require any special offset as all encoders were + staying within 12 bit without overflowing. ------------------------------------------------------------------------------- - (c) Copyright 2022 University of Liege + (c) Copyright 2022-2023 University of Liege Author: Thomas Lambert <t.lambert@uliege.be> ULiege - Aeroelasticity and Experimental Aerodynamics MIT License @@ -96,6 +99,10 @@ void GetAngleOffset(uint32_t calibTime) { } // Determine if offset is needed + // We know that the overall amplitude is approx 60deg -> ENC_PREICSION / 6 + // If we measure a difference between the min and max that is more than the + // preicsion/2, we are sure that we have an over/under flow somewhere. + // Therefore, we conclude that we need to offset everything. for (int i = 0; i < sizeof(gCSN_PINS); i++) { if (maxPos[i] - minPos[i] > ENC_PRECISION / 2) { gAngleOffset[i] = 1; diff --git a/controller/controller.ino b/controller/controller.ino index 8850336457707d9bb47a32e886b6df8b3e7cb2a3..d6288c70fbe36fb9020a238eb7a705719cf0a7b1 100644 --- a/controller/controller.ino +++ b/controller/controller.ino @@ -7,7 +7,7 @@ wings flap at the same frequency. The phase between the two sets will be adjusted manually by setting their initial positions as desired. ------------------------------------------------------------------------------ - (c) Copyright 2022 University of Liege + (c) Copyright 2022-2023 University of Liege Author: Thomas Lambert <t.lambert@uliege.be> ULiege - Aeroelasticity and Experimental Aerodynamics MIT License @@ -56,9 +56,9 @@ // Calibration of encoders #define ENC_PRECISION 4096 // 12-Bits encoders (nb of points per rot) #define ENC_ORIENT \ - { 1, -1, 1, -1 } // Encoder orientation + { 1, -1, -1, 1 } // Encoder orientation #define MAX_WING_POS \ - { 4095, 1317, 0, 1494 } // Maximum wing angle (returned by encoder value) + { 3166, 2374, 1290, 1135 } // Maximum wing angle (returned by encoder value) #define TRUE_MAX_ANGLE 38.07 // Maximum wing angle (measured on setup) @@ -89,7 +89,7 @@ Servo gEsc2; // ESC for aft motor uint8_t gEsc_val = 0; // ESC angle (to use with the Servo library) [0-180] String gMode; // Mode of operation -uint16_t gAngleOffset[] = { 0, 0, 1, 0 }; // Encoder offset (prevent wrap-around) +uint16_t gAngleOffset[] = { 0, 0, 0, 0 }; // Encoder offset (prevent wrap-around) uint16_t gMaxWingPos[] = MAX_WING_POS; // Encoder maximum recorder position int gStartTime; // Loop start time @@ -275,7 +275,7 @@ void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[ } for (int i = 0; i < sizeof(gCSN_PINS); i++) { Serial.print(","); - Serial.print(PosToDeg((wing_angles[i] - gMaxWingPos[i]) % ENC_PRECISION) + TRUE_MAX_ANGLE - 360); + Serial.print(PosToDeg(int(wing_angles[i]) - int(gMaxWingPos[i])) + TRUE_MAX_ANGLE); } for (int i = 0; i < 2; i++) { Serial.print(","); diff --git a/controller/sensors.ino b/controller/sensors.ino index 5fb742d6a83e9d0824af4a11ebefcd2707c5bf15..d15064991ad262fefeb22f9805a635e187f3bae2 100644 --- a/controller/sensors.ino +++ b/controller/sensors.ino @@ -3,8 +3,12 @@ Functions for sensors and acquisition. + WARNING: Not sure if the application of an offset to prevent integer overflow + is working properly or not. The second test campaign (Q3 2023) did not require + any special offset as all encoders were staying within 12 bit without + overflowing. ------------------------------------------------------------------------------ - (c) Copyright 2022 University of Liege + (c) Copyright 2022-2023 University of Liege Author: Thomas Lambert <t.lambert@uliege.be> ULiege - Aeroelasticity and Experimental Aerodynamics MIT License @@ -30,7 +34,19 @@ unsigned int ReadSensor(uint8_t csn, int8_t orient, int8_t offset) { digitalWrite(csn, HIGH); //deselects the encoder from reading - return (1 - orient) / 2 * ENC_PRECISION + orient * ret - (offset * ENC_PRECISION / 2); + // Flip the function if needed + ret = (1 - orient) / 2 * ENC_PRECISION + orient * ret; + + // Apply offset to prevent overflow if needed + // FIXME: Check. It is probably not correct + if (offset == 1) { + if (ret >= 0 and ret < ENC_PRECISION / 2) { + ret = ret + ENC_PRECISION / 2; + } else { + ret = ret - ENC_PRECISION / 2; + } + } + return ret; } // Get the tension and current of a PSU diff --git a/controller/utils.ino b/controller/utils.ino index d902aa535337c0cc48b29c3406ee19f1e3060235..f3fb116560c3f1d918676538aa3f54a461352a5f 100644 --- a/controller/utils.ino +++ b/controller/utils.ino @@ -3,7 +3,7 @@ Miscellaneous utilities. ------------------------------------------------------------------------------ - (c) Copyright 2022 University of Liege + (c) Copyright 2022-2023 University of Liege Author: Thomas Lambert <t.lambert@uliege.be> ULiege - Aeroelasticity and Experimental Aerodynamics MIT License @@ -32,7 +32,7 @@ float RpmToFreq(int rpm) { // Convert encoder position to degree -float PosToDeg(unsigned int pos) { +float PosToDeg(int pos) { return pos * 360.0 / ENC_PRECISION; }