Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

116 RC Threshold Calibration #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions RC_Calibration/Calibration_EEPROM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "pinout.h"
#include "array_function.h"
#include "threshold_function.h"
#include <stdlib.h>
#include <stdio.h>
#include <Console.h>

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
}
19 changes: 19 additions & 0 deletions RC_Calibration/EEPROM_address.h
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions RC_Calibration/array_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "array_function.h"
#include <stdlib.h>
#include <stdio.h>

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; i<n; i++){
for (j = 0; j<n - 1; j++){
if (array[j]>array[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;
}
22 changes: 22 additions & 0 deletions RC_Calibration/array_function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef array_function_H
#define array_function_H
#include <Arduino.h>

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
9 changes: 9 additions & 0 deletions RC_Calibration/pinout.h
Original file line number Diff line number Diff line change
@@ -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
152 changes: 152 additions & 0 deletions RC_Calibration/threshold_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include "EEPROM_address.h"
#include "pinout.h"
#include "array_function.h"
#include "threshold_function.h"
#include <EEPROM.h>
#include <Console.h>

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;
}
9 changes: 9 additions & 0 deletions RC_Calibration/threshold_function.h
Original file line number Diff line number Diff line change
@@ -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