From 8378d295b66d418b8cbf136ca3d0b889fed580c7 Mon Sep 17 00:00:00 2001
From: Thomas Lambert <t.lambert@uliege.be>
Date: Thu, 1 Dec 2022 13:59:54 +0100
Subject: [PATCH] fix: better offset and true calib

This commit adapts the calibration so it reflects the angles properly.
Note that the left wing sensor for the downstream module is not working.

It was also necessary to add a small offset to the first motor to keep
them more or less in sync. This offset seems to work quite decently.
---
 controller/calibration.ino |  6 +++++-
 controller/controller.ino  | 16 ++++++++--------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/controller/calibration.ino b/controller/calibration.ino
index a1ad945..7386153 100644
--- a/controller/calibration.ino
+++ b/controller/calibration.ino
@@ -50,7 +50,7 @@ void Calibrate() {
       gEsc2.write(0);
 
       Serial.println("[INFO]: Calibration done.");
-      Serial.println("Maxium wing posiions are: ");
+      Serial.println("Maximum wing positions are: ");
       PrintArray(gMaxWingPos);
 
       Serial.println("Offsets are: ");
@@ -60,6 +60,10 @@ void Calibrate() {
     }
   }
 
+  Serial.println("[INPUT]: Start acquisition then press 'y' to begin.");
+  while (Serial.available() == 0) {}  // Wait for serial input
+
+
   // Flush Serial before continuing
   while (Serial.available()) {
     Serial.read();
diff --git a/controller/controller.ino b/controller/controller.ino
index 83808ba..f37e6d3 100644
--- a/controller/controller.ino
+++ b/controller/controller.ino
@@ -58,14 +58,14 @@
 #define ENC_ORIENT \
   { 1, -1, 1, -1 }  // Encoder orientation
 #define MAX_WING_POS \
-  { 222.1, 117.77, 117, 117 }  // Maximum wing angle (returned by encoder value)
-#define TRUE_MAX_ANGLE 38.07   // Maximum wing angle (measured on setup)
+  { 4095, 1317, 0, 1494 }     // Maximum wing angle (returned by encoder value)
+#define TRUE_MAX_ANGLE 38.07  // Maximum wing angle (measured on setup)
 
 
 /*******************************************************************************
   Globals (for things that I can change manually)
  *******************************************************************************/
-const boolean gSKIP_CALIB = false;  // Skip calibration or not
+const boolean gSKIP_CALIB = true;  // Skip calibration or not
 
 // Constant values that can be tweaked here or in the ESC before run
 const uint16_t gMAX_SIG = 2000;  // Max signal to the ESC [ms]
@@ -75,7 +75,7 @@ const uint16_t gMAX_RPM = 3000;    // Motor maximum RPM (set in ESC)
 const uint16_t gMIN_RPM = 750;     // Motor minimum RPM (set in ESC)
 const uint8_t gMAX_ESC_VAL = 160;  // Value at which motor is at max RPM
 const uint8_t gMIN_ESC_VAL = 40;   // Min value for ESC to start motor (trial-error)
-const uint8_t gESC_OFFSET = 4;     // Offset to add to ESC 2 to sync them
+const uint8_t gESC_OFFSET = 1;     // Offset to add to ESC 1 to sync them
 
 const uint8_t gCSN_PINS[] = CSN_PINS;     // Encoder Chip Select pins
 const int8_t gENC_ORIENT[] = ENC_ORIENT;  // Encoder orientation
@@ -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, 0, 0 };  // Encoder offset (prevent wrap-around)
+uint16_t gAngleOffset[] = { 0, 0, 1, 0 };  // Encoder offset (prevent wrap-around)
 uint16_t gMaxWingPos[] = MAX_WING_POS;     // Encoder maximum recorder position
 
 
@@ -172,8 +172,8 @@ void loop() {
   }
 
   // Write value to ESCs
-  gEsc1.write(gEsc_val);
-  gEsc2.write(gEsc_val + gESC_OFFSET);
+  gEsc1.write(gEsc_val + gESC_OFFSET);
+  gEsc2.write(gEsc_val);
 
   // Read value from rotary encoders
   for (int i = 0; i < sizeof(gCSN_PINS); i++) {
@@ -234,7 +234,7 @@ void StopMotor(int old_esc_val) {
 
   Serial.println("[INFO] MOTOR STOP INITIATED");
 
-  if (old_esc_val > gMIN_ESC_VAL) {
+  if (old_esc_val > gMIN_ESC_VAL + 10) {
     Serial.println("[INFO] Slowing down motor for 3 sec...");
     gEsc1.write(gMIN_ESC_VAL);
     gEsc2.write(gMIN_ESC_VAL);
-- 
GitLab