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