diff --git a/controller/controller.ino b/controller/controller.ino index 0f6a6ffd00b789394536bb5ae235cb355526f99c..85ee2702e120681901d85c2ae605c59c6f3edb97 100644 --- a/controller/controller.ino +++ b/controller/controller.ino @@ -26,8 +26,9 @@ #define BAUD_RATE 115200 // baud rate for serial // Pins -#define ESC1_PIN 10 // ESC Front motor -#define ESC2_PIN 9 // ESC Aft motor +#define ESC1_PIN 10 // ESC Front motor +#define ESC2_PIN 9 // ESC Aft motor + #define SWITCH_PIN 7 // ON/OFF switch (mode control) #define POT_PIN A0 // Potentiometer @@ -42,17 +43,17 @@ #define TENS2_PIN A1 // Tension measurement for Aft motor // Values -#define DIV_RES1 48254.0 // Resistance of R1 in the voltage dividers +#define DIV_RES1 48254.0 // Resistance of R1 in the voltage dividers (calibrated so voltage matches) #define DIV_RES2 6800.0 // Resistance of R2 in the voltage dividers #define SEN0098_VCC 5 // Supply voltage of the current sensors #define SEN0098_SENSI 40.0 // Chip ACS758LCB-050B-PFF-T -#define SEN0098_QUIESCENT_FACTOR 0.5 // Bidirectional chip-> V_IOUTQ = 0.5*VCC -#define SEN0098_OFFSET 0.0122 // Manually set so current is 0 when no current +#define SEN0098_QUIESCENT_FACTOR 0.5 // Bidirectional chip -> V_IOUTQ = 0.5*VCC +#define SEN0098_OFFSET 0.0122 // Calibrated so current is 0 when no current #define GEAR_RATIO 0.1 // Gear ratio between motor and wings -// Calibration of encoders (NEEDS TO BE REDONE WHEN ENCODERS ARE MOVED) +// Calibration of encoders #define ENC_PRECISION 4096 // 12-Bits encoders (nb of points per rot) #define ENC_ORIENT \ { 1, -1, 1, -1 } // Encoder orientation @@ -118,10 +119,10 @@ void setup() { // Initialize modes int switch_state = digitalRead(SWITCH_PIN); + if (switch_state == LOW) { gMode = "MANUAL"; Serial.println("[WARNING]: Control motors using potentiometer"); - } else { gEsc1.write(0); // Must be initialized at 0 otherwise will not start gEsc2.write(0); @@ -137,7 +138,7 @@ void setup() { Calibrate(); } - delay(1000); + delay(1000); // Ensure proper initialization Serial.println("[INFO]: Setup OK, starting now..."); delay(500); } diff --git a/controller/sensors.ino b/controller/sensors.ino index 12a85f798d882159db382f917b2056191f75e26d..5fb742d6a83e9d0824af4a11ebefcd2707c5bf15 100644 --- a/controller/sensors.ino +++ b/controller/sensors.ino @@ -11,7 +11,8 @@ Repo: https://gitlab.uliege.be/thlamb/mecharaptor-controller *******************************************************************************/ -//Read 12-bit rotary encoder (RobinL modified by linarism on forum.arduino.cc) +// Read 12-bit rotary encoder +// Inspired from RobinL modified by linarism on forum.arduino.cc unsigned int ReadSensor(uint8_t csn, int8_t orient, int8_t offset) { unsigned int ret = 0; @@ -28,6 +29,7 @@ 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); } @@ -46,7 +48,7 @@ float GetCurrent(byte cur_pin) { return volt / gCUR_FACT; // Actual current } -// Measure the tension provided by the PSU +// Measure the tension provided by the PSU using the voltage divider float GetTension(byte vol_pin) { float vol_raw = analogRead(vol_pin) * (5.0 / 1023.0); diff --git a/controller/utils.ino b/controller/utils.ino index ef39dc39fc424821e10669a99ff3ad9d69591d1e..d902aa535337c0cc48b29c3406ee19f1e3060235 100644 --- a/controller/utils.ino +++ b/controller/utils.ino @@ -36,10 +36,11 @@ float PosToDeg(unsigned int pos) { return pos * 360.0 / ENC_PRECISION; } +// Print an array void PrintArray(int array[]) { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < sizeof(gCSN_PINS); i++) { Serial.print(array[i]); - if (i < 4 - 1) { + if (i < sizeof(gCSN_PINS) - 1) { Serial.print(", "); } }