From 5d05a3c82a6af375b96096ae49c4269b0ff812de Mon Sep 17 00:00:00 2001
From: Thomas Lambert <t.lambert@uliege.be>
Date: Fri, 4 Nov 2022 16:38:54 +0100
Subject: [PATCH] refact: reorder stuff, better names

---
 controller/controller.ino | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/controller/controller.ino b/controller/controller.ino
index 06d7a83..084ca39 100644
--- a/controller/controller.ino
+++ b/controller/controller.ino
@@ -32,7 +32,8 @@ Repo: https://gitlab.uliege.be/thlamb/mecharaptor-controller
 
 #define CLK_PIN 2  // Encoder CLK
 #define DO_PIN 8   // Encoder Digital Output
-#define CSN_PINS  { 3, 4, 5, 6 }  // Encoder Chip Select pins
+#define CSN_PINS \
+  { 3, 4, 5, 6 }  // Encoder Chip Select pins
 
 #define CUR1_PIN A1   // Current measurement for Front motor
 #define CUR2_PIN A2   // Current measurement for Aft motor
@@ -43,10 +44,10 @@ Repo: https://gitlab.uliege.be/thlamb/mecharaptor-controller
 #define DIV_RES1 9000.0  // Resistance of R1 in the voltage dividers
 #define DIV_RES2 1000.0  // Resistance of R2 in the voltage dividers
 
-#define SEN0098_VCC 5        // Supply voltage of the current sensors
-#define SEN0098_SENSI 20.0   // [FIXME] VERIFY!
-#define SEN0098_VIOUT 0.12   // [FIXME] VERIFY!
-#define SEN0098_OFFSET 0.12  // [FIXME] VERIFY!
+#define SEN0098_VCC 5         // Supply voltage of the current sensors
+#define SEN0098_SENSI 20.0    // [FIXME] VERIFY!
+#define SEN0098_VIOUT 0.12    // [FIXME] VERIFY!
+#define SEN0098_OFFSET 0.007  // [FIXME] VERIFY!
 
 #define GEAR_RATIO 0.1  // Gear ratio between motor and wings
 
@@ -159,8 +160,8 @@ void loop() {
 
     // Read current and power
     float psu_cur[2], psu_vol[2];
-    GetPsu(TENS1_PIN,CUR1_PIN, psu_vol[0], psu_cur[0]);
-    GetPsu(TENS2_PIN,CUR2_PIN, psu_vol[1], psu_cur[1]);
+    GetPsu(TENS1_PIN, CUR1_PIN, psu_vol[0], psu_cur[0]);
+    GetPsu(TENS2_PIN, CUR2_PIN, psu_vol[1], psu_cur[1]);
 
     SerialPrint(gMode, potent, gEsc_val, wing_angles, psu_vol, psu_cur);
 }
@@ -271,7 +272,9 @@ unsigned int ReadSensor(uint8_t csn) {
 
 
 // Output data to use with serial-studio
+// The /* and */ indicate beginning and end of stream data. Do not modify!
 void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[], float psu_vol[], float psu_cur[]) {
+
     float freq = map(esc_val, 0, 180, 0, RpmToFreq(gMAX_RPM) * 100.0) / 100.0;
 
     Serial.print("/*");
@@ -283,7 +286,7 @@ void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[
     Serial.print(",");
     Serial.print(esc_val);
     Serial.print(",");
-    Serial.print(freq);  // [FIXME] Is it useful?
+    Serial.print(freq);
     for (int i = 0; i < sizeof(gCSN_PINS); i++) {
         Serial.print(",");
         Serial.print(wing_angles[i]);
@@ -297,12 +300,19 @@ void SerialPrint(String mode, int potent, int esc_val, unsigned int wing_angles[
     Serial.println("*/");
 }
 
+
+// Get the tension and current of a PSU
+void GetPsu(byte vol_pin, byte cur_pin, float &vol, float &cur) {
+    vol = GetTension(vol_pin);
+    cur = GetCurrent(cur_pin);
+}
+
 // Measure current consumed by module
 float GetCurrent(byte cur_pin) {
-    float cur_raw = (5.0 / 1023.0) * analogRead(cur_pin);
+    float volt_raw = (5.0 / 1023.0) * analogRead(cur_pin);
 
-    float cur = cur_raw * -gCUR_QOV + 0.007;  //Trimming vale to make current equal to 0 when no current
-    return cur / gCUR_FACT;                   // get actual current
+    float volt = volt_raw * -gCUR_QOV + SEN0098_OFFSET;  // Trimming value to make current equal to 0 when no current
+    return volt / gCUR_FACT;                             // Actual current
 }
 
 // Measure the tension provided by the PSU
@@ -311,10 +321,3 @@ float GetTension(byte vol_pin) {
 
     return vol_raw * (DIV_RES1 + DIV_RES2) / (DIV_RES2);
 }
-
-// Get the tension and current of a PSU
-void GetPsu(byte vol_pin, byte cur_pin, float &vol, float &cur) {
-    vol = GetTension(vol_pin);
-    cur=  GetCurrent(cur_pin);
-
-}
-- 
GitLab