From 4dc9af3ee8eadc6718d113369c7b68e78a7095d4 Mon Sep 17 00:00:00 2001 From: zhaoshengEE Date: Fri, 20 Mar 2020 17:02:28 -0700 Subject: [PATCH 1/3] RC Threshold Calibration --- Calibration.ino | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Calibration.ino diff --git a/Calibration.ino b/Calibration.ino new file mode 100644 index 00000000..9a9567b3 --- /dev/null +++ b/Calibration.ino @@ -0,0 +1,96 @@ +#include + +int rc_channel2 = 3; +int rc_channel3 = 4; +int switchA = 6; //Channel 5 +int switchB = 7; //Channel 6 +File script; + +void setup() { + // put your setup code here, to run once: + pinMode(rc_channel2, INPUT); + pinMode(rc_channel3, INPUT); + pinMode(switchA, INPUT); + pinMode(switchB, INPUT); + FileSystem.begin(); + File script = FileSystem.open("RC_Threshold.txt", FILE_WRITE); + Serial.begin(9600); +} + +void loop() { + int rc2; //Right Joystick + int rc3; //Left Joystick + int swa; //Channel 5 (Remote E-stop) + int swb; //Channel 6 (Mode Switch) + +//**********Right Joystick**********// + //MAX Position + Serial.println("Please move the right joystick to the maximum position\n"); + script.print("Righ Joystick\n"); + delay(2000); + rc2 = pulseIn(rc_channel2, HIGH); + script.print("MAX: "); script.println(rc2); script.print("\n"); + + //MIN Position + Serial.println("Please move the right joystick to the minimum position\n"); + delay(2000); + rc2 = pulseIn(rc_channel2, HIGH); + script.print("MIN: "); script.println(rc2); script.print("\n"); + + //Central Position + Serial.println("Please move the right joystick to the central position\n"); + delay(2000); + rc2 = pulseIn(rc_channel2, HIGH); + script.print("Central: "); script.println(rc2); script.print("\n"); + +//**********Left Joystick**********// + //MAX Position + Serial.println("Please move the left joystick to the maximum position\n"); + script.print("Left Joystick\n"); + delay(2000); + rc3 = pulseIn(rc_channel3, HIGH); + script.print("MAX: "); script.println(rc3); script.print("\n"); + + //MIN Position + Serial.println("Please move the left joystick to the minimum position\n"); + delay(2000); + rc3 = pulseIn(rc_channel3, HIGH); + script.print("MIN: "); script.println(rc3); script.print("\n"); + + //Central Position + Serial.println("Please move the left joystick to the central position\n"); + delay(2000); + rc3 = pulseIn(rc_channel3, HIGH); + script.print("Central: "); script.println(rc3); script.print("\n"); + +//**********Switch A(Remote E-stop)**********// + //Position 0 + Serial.println("Please move Switch A to 0\n"); + script.print("Switch A\n"); + delay(2000); + swa = pulseIn(switchA, HIGH); + script.print("MAX: "); script.println(swa); script.print("\n"); + + //Position 1 + Serial.println("Please move Switch A to 1\n"); + delay(2000); + swa = pulseIn(switchA, HIGH); + script.print("MIN: "); script.println(swa); script.print("\n"); + +//**********Switch B(Mode Switch)**********// + //Position 0 + Serial.println("Please move Switch B to 0\n"); + script.print("Switch B\n"); + delay(2000); + swb = pulseIn(switchB, HIGH); + script.print("MAX: "); script.println(swb); script.print("\n"); + + //Position 1 + Serial.println("Please move Switch B to 1\n"); + delay(2000); + swb = pulseIn(switchB, HIGH); + script.print("MIN: "); script.println(swb); script.print("\n"); + + script.close(); + for(;;){} //stop the loop function +} From bbf3a4106ce61dae109bd3b5af3a3918297498a0 Mon Sep 17 00:00:00 2001 From: zhaoshengEE Date: Thu, 26 Mar 2020 18:21:41 -0700 Subject: [PATCH 2/3] RC Threshold Calibration with EEPROM --- Calibration.ino => Calibration_EEPROM.ino | 48 ++++++++++++----------- 1 file changed, 26 insertions(+), 22 deletions(-) rename Calibration.ino => Calibration_EEPROM.ino (68%) diff --git a/Calibration.ino b/Calibration_EEPROM.ino similarity index 68% rename from Calibration.ino rename to Calibration_EEPROM.ino index 9a9567b3..2a8b2d22 100644 --- a/Calibration.ino +++ b/Calibration_EEPROM.ino @@ -1,10 +1,23 @@ -#include - +#include int rc_channel2 = 3; int rc_channel3 = 4; int switchA = 6; //Channel 5 int switchB = 7; //Channel 6 -File script; + +//Address in EEPROM +const int rc2_addr_max = 0; +const int rc2_addr_min = 1; +const int rc2_addr_central = 2; + +const int rc3_addr_max = 3; +const int rc3_addr_min = 4; +const int rc3_addr_central = 5; + +const int rc5_addr_zero = 6; +const int rc5_addr_one = 7; + +const int rc6_addr_zero = 8; +const int rc6_addr_one = 9; void setup() { // put your setup code here, to run once: @@ -12,8 +25,6 @@ void setup() { pinMode(rc_channel3, INPUT); pinMode(switchA, INPUT); pinMode(switchB, INPUT); - FileSystem.begin(); - File script = FileSystem.open("RC_Threshold.txt", FILE_WRITE); Serial.begin(9600); } @@ -26,71 +37,64 @@ void loop() { //**********Right Joystick**********// //MAX Position Serial.println("Please move the right joystick to the maximum position\n"); - script.print("Righ Joystick\n"); delay(2000); rc2 = pulseIn(rc_channel2, HIGH); - script.print("MAX: "); script.println(rc2); script.print("\n"); - + EEPROM.put(rc2_addr_max, rc2); //MIN Position Serial.println("Please move the right joystick to the minimum position\n"); delay(2000); rc2 = pulseIn(rc_channel2, HIGH); - script.print("MIN: "); script.println(rc2); script.print("\n"); + EEPROM.put(rc2_addr_min, rc2); //Central Position Serial.println("Please move the right joystick to the central position\n"); delay(2000); rc2 = pulseIn(rc_channel2, HIGH); - script.print("Central: "); script.println(rc2); script.print("\n"); + EEPROM.put(rc2_addr_central, rc2); //**********Left Joystick**********// //MAX Position Serial.println("Please move the left joystick to the maximum position\n"); - script.print("Left Joystick\n"); delay(2000); rc3 = pulseIn(rc_channel3, HIGH); - script.print("MAX: "); script.println(rc3); script.print("\n"); + EEPROM.put(rc3_addr_max, rc3); //MIN Position Serial.println("Please move the left joystick to the minimum position\n"); delay(2000); rc3 = pulseIn(rc_channel3, HIGH); - script.print("MIN: "); script.println(rc3); script.print("\n"); + EEPROM.put(rc3_addr_min, rc3); //Central Position Serial.println("Please move the left joystick to the central position\n"); delay(2000); rc3 = pulseIn(rc_channel3, HIGH); - script.print("Central: "); script.println(rc3); script.print("\n"); + EEPROM.put(rc3_addr_central, rc3); //**********Switch A(Remote E-stop)**********// //Position 0 Serial.println("Please move Switch A to 0\n"); - script.print("Switch A\n"); delay(2000); swa = pulseIn(switchA, HIGH); - script.print("MAX: "); script.println(swa); script.print("\n"); - + EEPROM.put(rc5_addr_zero, swa); //Position 1 Serial.println("Please move Switch A to 1\n"); delay(2000); swa = pulseIn(switchA, HIGH); - script.print("MIN: "); script.println(swa); script.print("\n"); + EEPROM.put(rc5_addr_one, swa); //**********Switch B(Mode Switch)**********// //Position 0 Serial.println("Please move Switch B to 0\n"); - script.print("Switch B\n"); delay(2000); swb = pulseIn(switchB, HIGH); - script.print("MAX: "); script.println(swb); script.print("\n"); + EEPROM.put(rc6_addr_zero, swb); //Position 1 Serial.println("Please move Switch B to 1\n"); delay(2000); swb = pulseIn(switchB, HIGH); - script.print("MIN: "); script.println(swb); script.print("\n"); + EEPROM.put(rc6_addr_one, swb); - script.close(); for(;;){} //stop the loop function } From e169eb6edffb7ab075cc5da076f421af39e07bd4 Mon Sep 17 00:00:00 2001 From: zhaoshengEE Date: Sat, 28 Mar 2020 21:15:05 -0700 Subject: [PATCH 3/3] RC_Calibration_V2.0 --- Calibration_EEPROM.ino | 100 ----------------- RC_Calibration/Calibration_EEPROM.ino | 52 +++++++++ RC_Calibration/EEPROM_address.h | 19 ++++ RC_Calibration/array_function.cpp | 54 +++++++++ RC_Calibration/array_function.h | 22 ++++ RC_Calibration/pinout.h | 9 ++ RC_Calibration/threshold_function.cpp | 152 ++++++++++++++++++++++++++ RC_Calibration/threshold_function.h | 9 ++ 8 files changed, 317 insertions(+), 100 deletions(-) delete mode 100644 Calibration_EEPROM.ino create mode 100644 RC_Calibration/Calibration_EEPROM.ino create mode 100644 RC_Calibration/EEPROM_address.h create mode 100644 RC_Calibration/array_function.cpp create mode 100644 RC_Calibration/array_function.h create mode 100644 RC_Calibration/pinout.h create mode 100644 RC_Calibration/threshold_function.cpp create mode 100644 RC_Calibration/threshold_function.h diff --git a/Calibration_EEPROM.ino b/Calibration_EEPROM.ino deleted file mode 100644 index 2a8b2d22..00000000 --- a/Calibration_EEPROM.ino +++ /dev/null @@ -1,100 +0,0 @@ -#include -int rc_channel2 = 3; -int rc_channel3 = 4; -int switchA = 6; //Channel 5 -int switchB = 7; //Channel 6 - -//Address in EEPROM -const int rc2_addr_max = 0; -const int rc2_addr_min = 1; -const int rc2_addr_central = 2; - -const int rc3_addr_max = 3; -const int rc3_addr_min = 4; -const int rc3_addr_central = 5; - -const int rc5_addr_zero = 6; -const int rc5_addr_one = 7; - -const int rc6_addr_zero = 8; -const int rc6_addr_one = 9; - -void setup() { - // put your setup code here, to run once: - pinMode(rc_channel2, INPUT); - pinMode(rc_channel3, INPUT); - pinMode(switchA, INPUT); - pinMode(switchB, INPUT); - Serial.begin(9600); -} - -void loop() { - int rc2; //Right Joystick - int rc3; //Left Joystick - int swa; //Channel 5 (Remote E-stop) - int swb; //Channel 6 (Mode Switch) - -//**********Right Joystick**********// - //MAX Position - Serial.println("Please move the right joystick to the maximum position\n"); - delay(2000); - rc2 = pulseIn(rc_channel2, HIGH); - EEPROM.put(rc2_addr_max, rc2); - //MIN Position - Serial.println("Please move the right joystick to the minimum position\n"); - delay(2000); - rc2 = pulseIn(rc_channel2, HIGH); - EEPROM.put(rc2_addr_min, rc2); - - //Central Position - Serial.println("Please move the right joystick to the central position\n"); - delay(2000); - rc2 = pulseIn(rc_channel2, HIGH); - EEPROM.put(rc2_addr_central, rc2); - -//**********Left Joystick**********// - //MAX Position - Serial.println("Please move the left joystick to the maximum position\n"); - delay(2000); - rc3 = pulseIn(rc_channel3, HIGH); - EEPROM.put(rc3_addr_max, rc3); - - //MIN Position - Serial.println("Please move the left joystick to the minimum position\n"); - delay(2000); - rc3 = pulseIn(rc_channel3, HIGH); - EEPROM.put(rc3_addr_min, rc3); - - //Central Position - Serial.println("Please move the left joystick to the central position\n"); - delay(2000); - rc3 = pulseIn(rc_channel3, HIGH); - EEPROM.put(rc3_addr_central, rc3); - -//**********Switch A(Remote E-stop)**********// - //Position 0 - Serial.println("Please move Switch A to 0\n"); - delay(2000); - swa = pulseIn(switchA, HIGH); - EEPROM.put(rc5_addr_zero, swa); - //Position 1 - Serial.println("Please move Switch A to 1\n"); - delay(2000); - swa = pulseIn(switchA, HIGH); - EEPROM.put(rc5_addr_one, swa); - -//**********Switch B(Mode Switch)**********// - //Position 0 - Serial.println("Please move Switch B to 0\n"); - delay(2000); - swb = pulseIn(switchB, HIGH); - EEPROM.put(rc6_addr_zero, swb); - - //Position 1 - Serial.println("Please move Switch B to 1\n"); - delay(2000); - swb = pulseIn(switchB, HIGH); - EEPROM.put(rc6_addr_one, swb); - - for(;;){} //stop the loop function -} diff --git a/RC_Calibration/Calibration_EEPROM.ino b/RC_Calibration/Calibration_EEPROM.ino new file mode 100644 index 00000000..86386fc2 --- /dev/null +++ b/RC_Calibration/Calibration_EEPROM.ino @@ -0,0 +1,52 @@ +#include "pinout.h" +#include "array_function.h" +#include "threshold_function.h" +#include +#include +#include + +void setup() { + Console.begin(); // Initialize Console + while (!Console); // Wait for the Console port to connect + + pinMode(rc_channel2, INPUT); + pinMode(rc_channel3, INPUT); + pinMode(switch_A, INPUT); + pinMode(switch_B, INPUT); + Serial.begin(9600); +} + +void loop() { + int rc2; //Right Joystick + int rc3; //Left Joystick + int swa; //Channel 5 (Remote E-stop) + int swb; //Channel 6 (Mode Switch) + + Array channel2; //Temporary array to store values from Channel 2 + Array channel3; //Temporary array to store values from Channel 3 + Array switchA; //Temporary array to store values from Switch A + Array switchB; //Temporary array to store values from Switch B + +//Initilize the arrays above with initial size of 5 + initArray(&channel2, 5); + initArray(&channel3, 5); + initArray(&switchA, 5); + initArray(&switchB, 5); + + Serial.println("Please put both joysticks at the central position.\n"); + Serial.println("Please put all the switchs at 0 position.\n"); + + //Measure and store the threshold value in EEPROM + get_right_threshold(&channel2); + get_left_threshold(&channel3); + get_switchA_threshold(&switchA); + get_switchB_threshold(&switchB); + + //clear all the temporary array + freeArray(&channel2); + freeArray(&channel3); + freeArray(&switchA); + freeArray(&switchB); + + for(;;){} //stop the loop function +} diff --git a/RC_Calibration/EEPROM_address.h b/RC_Calibration/EEPROM_address.h new file mode 100644 index 00000000..9ec386a5 --- /dev/null +++ b/RC_Calibration/EEPROM_address.h @@ -0,0 +1,19 @@ +#ifndef EEPROM_address_H +#define EEPROM_address_H + +//Address in EEPROM +#define rc2_addr_max 0 +#define rc2_addr_min 1 +#define rc2_addr_central 2 + +#define rc3_addr_max 3 +#define rc3_addr_min 4 +#define rc3_addr_central 5 + +#define rc5_addr_zero 6 +#define rc5_addr_one 7 + +#define rc6_addr_zero 8 +#define rc6_addr_one 9 + +#endif // EEPROM_address_H diff --git a/RC_Calibration/array_function.cpp b/RC_Calibration/array_function.cpp new file mode 100644 index 00000000..ff3a1dbf --- /dev/null +++ b/RC_Calibration/array_function.cpp @@ -0,0 +1,54 @@ +#include "array_function.h" +#include +#include + +void initArray(Array *a, size_t initialSize) { + a->array = (int *)malloc(initialSize * sizeof(int)); + a->used = 0; + a->size = initialSize; +} + +void insertArray(Array *a, int element) { + if (a->used == a->size) { + a->size += 1; + a->array = (int *)realloc(a->array, a->size * sizeof(int)); + } + a->array[a->used++] = element; +} + +void freeArray(Array *a) { + free(a->array); + a->array = NULL; + a->used = a->size = 0; +} + +// function to sort the array in ascending order +void Array_sort(int *array, size_t n) +{ + int i = 0, j = 0, temp = 0; + + for (i = 0; iarray[j + 1]){ + temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } +} + +// function to calculate the median of the array +int Find_median(int array[], size_t n) +{ + int median = 0; + + // if number of elements are even + if (n % 2 == 0) + median = (array[(n - 1) / 2] + array[n / 2]) / 2; + // if number of elements are odd + else + median = array[n / 2]; + + return median; +} \ No newline at end of file diff --git a/RC_Calibration/array_function.h b/RC_Calibration/array_function.h new file mode 100644 index 00000000..48680344 --- /dev/null +++ b/RC_Calibration/array_function.h @@ -0,0 +1,22 @@ +#ifndef array_function_H +#define array_function_H +#include + +typedef struct { + int *array; + size_t used; + size_t size; +} Array; + +void initArray(Array *a, size_t initialSize); + +void insertArray(Array *a, int element); + +void freeArray(Array *a); + +void Array_sort(int *array, size_t n); + +int Find_median(int array[], size_t n); + + +#endif // array_function_H diff --git a/RC_Calibration/pinout.h b/RC_Calibration/pinout.h new file mode 100644 index 00000000..78134f19 --- /dev/null +++ b/RC_Calibration/pinout.h @@ -0,0 +1,9 @@ +#ifndef pinout_H +#define pinout_H + +#define rc_channel2 3 //Channel 2 +#define rc_channel3 4 //Channel 3 +#define switch_A 6 //Channel 5 +#define switch_B 7 //Channel 6 + +#endif // pinout_H diff --git a/RC_Calibration/threshold_function.cpp b/RC_Calibration/threshold_function.cpp new file mode 100644 index 00000000..b5c6c65f --- /dev/null +++ b/RC_Calibration/threshold_function.cpp @@ -0,0 +1,152 @@ +#include "EEPROM_address.h" +#include "pinout.h" +#include "array_function.h" +#include "threshold_function.h" +#include +#include + +char action = 'O'; + +//**********Right Joystick**********// +void get_right_threshold(Array *a){ + int rc2_max, rc2_min, rc2_central; + + //MAX Position + Serial.println("Please move the right joystick to the most forward position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while(action == 'S'){ + insertArray(a, pulseIn(rc_channel2, HIGH)); + } + + //MIN Position + Serial.println("Please move the right joystick to the most backward position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(rc_channel2, HIGH)); + } + + //Central Position + Serial.println("Please move the right joystick to the central position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(rc_channel2, HIGH)); + } + + Array_sort(a->array, a->size); + rc2_max = a->array[a->size]; + rc2_min = a->array[0]; + rc2_central = Find_median(a->array, a->size); + + EEPROM.put(rc2_addr_max, rc2_max); + EEPROM.put(rc2_addr_min, rc2_min); + EEPROM.put(rc2_addr_central, rc2_central); + + freeArray(a); + return; +} + +////**********Left Joystick**********// +void get_left_threshold(Array *a){ + int rc3_max, rc3_min, rc3_central; + + //MAX Position + Serial.println("Please move the left joystick to the most forward position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(rc_channel3, HIGH)); + } + + //MIN Position + Serial.println("Please move the left joystick to the most backward position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(rc_channel3, HIGH)); + } + + //Central Position + Serial.println("Please move the left joystick to the central position.\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(rc_channel3, HIGH)); + } + + Array_sort(a->array, a->size); + rc3_max = a->array[a->size]; + rc3_min = a->array[0]; + rc3_central = Find_median(a->array, a->size); + + EEPROM.put(rc3_addr_max, rc3_max); + EEPROM.put(rc3_addr_min, rc3_min); + EEPROM.put(rc3_addr_central, rc3_central); + + freeArray(a); + return; +} + +//**********Switch A(Remote E-stop)**********// +void get_switchA_threshold(Array *a){ + int swa_max, swa_min; + + //Position 0 + Serial.println("Please move Switch A to 0\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(switch_A, HIGH)); + } + + //Position 1 + Serial.println("Please move Switch A to 1\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(switch_A, HIGH)); + } + + Array_sort(a->array, a->size); + swa_max = a->array[a->size]; + swa_min = a->array[0]; + + EEPROM.put(rc5_addr_zero, swa_max); + EEPROM.put(rc5_addr_one, swa_min); + + freeArray(a); + return; +} + +//**********Switch B(Mode Switch)**********// +void get_switchB_threshold(Array *a){ + int swb_max, swb_min; + + //Position 0 + Serial.println("Please move Switch B to 0\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(switch_B, HIGH)); + } + + //Position 1 + Serial.println("Please move Switch B to 1\n"); + Serial.println("Type in 'S' to start measurement or type in 'E' to end measurement.\n"); + action = Console.read(); + while (action == 'S') { + insertArray(a, pulseIn(switch_B, HIGH)); + } + + Array_sort(a->array, a->size); + swb_max = a->array[a->size]; + swb_min = a->array[0]; + + EEPROM.put(rc6_addr_zero, swb_max); + EEPROM.put(rc6_addr_one, swb_min); + + freeArray(a); + return; +} diff --git a/RC_Calibration/threshold_function.h b/RC_Calibration/threshold_function.h new file mode 100644 index 00000000..ba5b4400 --- /dev/null +++ b/RC_Calibration/threshold_function.h @@ -0,0 +1,9 @@ +#ifndef threshold_function_H +#define threshold_function_H + +void get_right_threshold(Array *a); +void get_left_threshold(Array *a); +void get_switchA_threshold(Array *a); +void get_switchB_threshold(Array *a); + +#endif // threshold_function_H