Skip to content
Snippets Groups Projects
Verified Commit c41a98ba authored by Thomas Lambert's avatar Thomas Lambert :helicopter:
Browse files

fix(encoders): multiple issues with encoders

- Issue with interger overlow is no longer a problem. I tried to work it
  out, but as the sensors are all within range for the Q3 2023 campaign,
  I have not gone deeper into it.
- Issue with conversion between bit value and degrees properly solved.

Other changes concern calibration value to display data properly for the
Q3 2023 test campaign.
parent 9f5a079e
No related branches found
No related tags found
No related merge requests found
...@@ -7,8 +7,11 @@ ...@@ -7,8 +7,11 @@
Then we determine the maximum position so we can make it match the maximum Then we determine the maximum position so we can make it match the maximum
angle attainable. 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> Author: Thomas Lambert <t.lambert@uliege.be>
ULiege - Aeroelasticity and Experimental Aerodynamics ULiege - Aeroelasticity and Experimental Aerodynamics
MIT License MIT License
...@@ -96,6 +99,10 @@ void GetAngleOffset(uint32_t calibTime) { ...@@ -96,6 +99,10 @@ void GetAngleOffset(uint32_t calibTime) {
} }
// Determine if offset is needed // 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++) { for (int i = 0; i < sizeof(gCSN_PINS); i++) {
if (maxPos[i] - minPos[i] > ENC_PRECISION / 2) { if (maxPos[i] - minPos[i] > ENC_PRECISION / 2) {
gAngleOffset[i] = 1; gAngleOffset[i] = 1;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
wings flap at the same frequency. The phase between the two sets will be wings flap at the same frequency. The phase between the two sets will be
adjusted manually by setting their initial positions as desired. 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> Author: Thomas Lambert <t.lambert@uliege.be>
ULiege - Aeroelasticity and Experimental Aerodynamics ULiege - Aeroelasticity and Experimental Aerodynamics
MIT License MIT License
...@@ -56,9 +56,9 @@ ...@@ -56,9 +56,9 @@
// Calibration of encoders // Calibration of encoders
#define ENC_PRECISION 4096 // 12-Bits encoders (nb of points per rot) #define ENC_PRECISION 4096 // 12-Bits encoders (nb of points per rot)
#define ENC_ORIENT \ #define ENC_ORIENT \
{ 1, -1, 1, -1 } // Encoder orientation { 1, -1, -1, 1 } // Encoder orientation
#define MAX_WING_POS \ #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) #define TRUE_MAX_ANGLE 38.07 // Maximum wing angle (measured on setup)
...@@ -89,7 +89,7 @@ Servo gEsc2; // ESC for aft motor ...@@ -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] uint8_t gEsc_val = 0; // ESC angle (to use with the Servo library) [0-180]
String gMode; // Mode of operation 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 uint16_t gMaxWingPos[] = MAX_WING_POS; // Encoder maximum recorder position
int gStartTime; // Loop start time int gStartTime; // Loop start time
...@@ -275,7 +275,7 @@ void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[ ...@@ -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++) { for (int i = 0; i < sizeof(gCSN_PINS); i++) {
Serial.print(","); 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++) { for (int i = 0; i < 2; i++) {
Serial.print(","); Serial.print(",");
......
...@@ -3,8 +3,12 @@ ...@@ -3,8 +3,12 @@
Functions for sensors and acquisition. 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> Author: Thomas Lambert <t.lambert@uliege.be>
ULiege - Aeroelasticity and Experimental Aerodynamics ULiege - Aeroelasticity and Experimental Aerodynamics
MIT License MIT License
...@@ -30,7 +34,19 @@ unsigned int ReadSensor(uint8_t csn, int8_t orient, int8_t offset) { ...@@ -30,7 +34,19 @@ unsigned int ReadSensor(uint8_t csn, int8_t orient, int8_t offset) {
digitalWrite(csn, HIGH); //deselects the encoder from reading 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 // Get the tension and current of a PSU
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Miscellaneous utilities. Miscellaneous utilities.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
(c) Copyright 2022 University of Liege (c) Copyright 2022-2023 University of Liege
Author: Thomas Lambert <t.lambert@uliege.be> Author: Thomas Lambert <t.lambert@uliege.be>
ULiege - Aeroelasticity and Experimental Aerodynamics ULiege - Aeroelasticity and Experimental Aerodynamics
MIT License MIT License
...@@ -32,7 +32,7 @@ float RpmToFreq(int rpm) { ...@@ -32,7 +32,7 @@ float RpmToFreq(int rpm) {
// Convert encoder position to degree // Convert encoder position to degree
float PosToDeg(unsigned int pos) { float PosToDeg(int pos) {
return pos * 360.0 / ENC_PRECISION; return pos * 360.0 / ENC_PRECISION;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment