diff --git a/src/Filter.h b/src/Filter.h deleted file mode 100644 index ef36fbf..0000000 --- a/src/Filter.h +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include "MedianFilter.h" - -// Create median filter objects, once for each sensor -MedianFilter mf0; -MedianFilter mf1; -//MedianFilter mf2; -//MedianFilter mf3; -//MedianFilter mf4; -//MedianFilter mf5; -//MedianFilter mf6; -//MedianFilter mf7; -//MedianFilter mf8; -//MedianFilter mf9; - -float getFilteredAngle(float fingerAngles,int i){ - - //thumbIP_angle, - if (i == 0){ - mf0.addSample(fingerAngles); - float FilterAngle0 = mf0.getMedian(); - return FilterAngle0; - } - - // thumbMCP_angle - else if (i == 1){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - /* - // f1PIP_angle - else if (i == 2){ - mf1.addSample(fingerAngles); - float FilterAngle2 = mf1.getMedian(); - return FilterAngle2; - } - - // f1MCP_angle - else if (i == 3){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f2PIP_angle - else if (i == 4){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f2MCP_angle - else if (i == 5){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f3PIP_angle - else if (i == 6){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f3MCP_angle - else if (i == 7){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f4PIP_angle - else if (i == 8){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - - // f4MCP_angle - else if (i == 9){ - mf1.addSample(fingerAngles); - float FilterAngle1 = mf1.getMedian(); - return FilterAngle1; - } - */ -} \ No newline at end of file diff --git a/src/FilterTest.cpp b/src/FilterTest.cpp deleted file mode 100644 index c83c842..0000000 --- a/src/FilterTest.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include "Filter.h" - - -const int flexPin1 = 4; // Pin connected to voltage divider output -const int flexPin2 = 0; // Pin connected to voltage divider output -float fingerAngles[2] = {0,0}; -float fingerAnglesFiltered[2] = {0,0}; -const int sizeList = 2; - -// Change these constants according to your project's design -const float VCC = 5; // voltage at Ardunio 5V line -const float R_DIV = 10000.0; // resistor used to create a voltage divider - -void setup() { - Serial.begin(115200); - pinMode(flexPin1, INPUT); - pinMode(flexPin2, INPUT); -} - - -void loop() { - - // Calculate angle of fingers - int ADCflex0 = analogRead(flexPin1); - int ADCflex1 = analogRead(flexPin2); - - float Vflex0 = ADCflex0 * VCC / 4095.0; //1023.0 - float Vflex1 = ADCflex1 * VCC / 4095.0; //1023.0 - - float Rflex0 = R_DIV * (VCC / Vflex0 - 1.0); - float Rflex1 = R_DIV * (VCC / Vflex1 - 1.0); - - float angle0 = Rflex0 * 2.6716/1000 - 30.2045 -20; - float angle1 = Rflex1 * 2.6716/1000 - 30.2045 -20; - - fingerAngles[0] = angle0; - fingerAngles[1] = angle1; - - - //Läser alla sensorer och lägger i en lista som sedan filtreras - for(int i = 0; i -#include "config.h" -#include "MedianFilter.h" - -/* - Main class -*/ -class flexsensor{ - // Variables -private: - int m_pin; - String m_location; - MedianFilter m_f; - -public: - flexsensor(int pin, String location){}; - // Methods - // ... Gets - float getMedian(); - String getlocation(); - // ... Sets - void read(); -}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d816d39..51c0b8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,45 +1,37 @@ #include -#include -#include - -#define interuptPin 17 +#include "TalkyBoi.h" // Pin connected to voltage divider output. used for analogRead to get resistance of the flexsensors -const int thumbIP_Pin = 36; -const int thumbMCP_Pin = 39; -const int f1PIP_Pin = 32; -const int f1MCP_Pin = 33; -const int f2PIP_Pin = 34; -const int f2MCP_Pin = 35; -const int f3PIP_Pin = ads1118.AIN_2; -const int f3MCP_Pin = ads1118.AIN_3; -const int f4PIP_Pin = ads1118.AIN_0; -const int f4MCP_Pin = ads1118.AIN_1; +const uint8_t thumbIP_Pin = ads1118.AIN_0; +const uint8_t thumbMCP_Pin = ads1118.AIN_1; +const uint8_t f1PIP_Pin = ads1118.AIN_2; +const uint8_t f1MCP_Pin = ads1118.AIN_3; +const int f2PIP_Pin = 36; +const int f2MCP_Pin = 39; +const int f3PIP_Pin = 32; +const int f3MCP_Pin = 33; +const int f4PIP_Pin = 34; +const int f4MCP_Pin = 35; const int pinList[] = {thumbIP_Pin, thumbMCP_Pin, f1PIP_Pin, f1MCP_Pin, f2PIP_Pin, f2MCP_Pin, f3PIP_Pin, f3MCP_Pin, f4PIP_Pin, f4MCP_Pin}; const int sizeList = sizeof(pinList)/sizeof(int); float fingerAngles[10] = {0,0,0,0,0,0,0,0,0,0}; int sendID = 0; -int o = 0; int buttonRun = 0; int state = 0; +/** + * Initiates the ADC breakout board. +*/ void initBoard(){ ads1118.begin(); // Initialize board ads1118.setSamplingRate(ads1118.RATE_860SPS); // highest sampling rate possible ads1118.setFullScaleRange(ads1118.FSR_4096); // 12 bit } -void initAnalogPin(){ - pinMode(thumbIP_Pin, INPUT); // change later! - pinMode(thumbMCP_Pin, INPUT); - pinMode(f1PIP_Pin, INPUT); - pinMode(f1MCP_Pin, INPUT); - pinMode(f2PIP_Pin, INPUT); - pinMode(f2MCP_Pin, INPUT); - -} - +/** + * Toggles the state of the microcontroller. +*/ void interuptFunc(){ static unsigned long last_interuptTime = 0; unsigned long interupt_time = millis(); @@ -56,43 +48,22 @@ void interuptFunc(){ } - void setup() { Serial.begin(115200); delay(100); - getMACAdress(); //MAC adress är vad som körs för att WIFI ska funkar, den ANDRA bärands MAC adress ska skrivas in i denna koden och tvärtom. + getMACAdress(); //MAC adress is run for the WIFI to work, the OTHER wearers MAC adress shall be written in this code and vice versa. init_wifi(); // Initiate ESP_NOW - initBoard(); // Initiate breakout board - initAnalogPin(); - // attachInterrupt(17, interuptFunc, HIGH); // interupt för start/stopp knapp + initBoard(); // Initiate breakout board + //attachInterrupt(17, interuptFunc, HIGH); // interupt for start/stop button } void loop() { - //while(!state){} //Ta bort kommentar för att ha en knapp som låser/öppnar programmet när det körs. Behöver en debounce för knappen innan det funkar. - - for(int i = 0; i>>>>>> de77f9514885061b4545c7e60c6282d2ad28d74c + /* sendID++; send(sendID, fingerAngles[0], fingerAngles[1], fingerAngles[2], fingerAngles[3], fingerAngles[4], fingerAngles[5], fingerAngles[6], fingerAngles[7], fingerAngles[8], fingerAngles[9]); -<<<<<<< HEAD - - sendToModel(o,1.0,1.0); - o++; - delay(1000); - -======= - delay(10); - delay(10); ->>>>>>> de77f9514885061b4545c7e60c6282d2ad28d74c + */ + delay(100); } diff --git a/src/readFingers.h b/src/readFingers.h deleted file mode 100644 index 5d6a209..0000000 --- a/src/readFingers.h +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include - -// Defining breakout board, multiplexter -#define CS 5 -ADS1118 ads1118(CS); - -// Change these constants according to your project's design -const float VCC = 3.3; // voltage at Ardunio 5V line -const float R_DIV = 100000.0; // resistor used to create a voltage divider - -float readResistance(int pin, int type){ // 0<=type<6 -> on esp board, 6= on multiplexer - float Vflex = 0; - if(type < 6){ - int ADCflex = analogRead(pin); - Vflex = ADCflex * VCC / 4095.0; // 12 bit gives 4095 values - } - else if (type >= 6){ - Vflex = ads1118.getMilliVolts(pin) / 1000.0; - } - - float Rflex = R_DIV * (VCC / Vflex - 1.0); - return Rflex; -} - -float getAngle(int pin, int type){ - float angle = readResistance(pin, type) * 2.6716/1000 - 30.2045 - 60; //From Matlab calibration - return angle; -} - diff --git a/src/sensorClass.cpp b/src/sensorClass.cpp new file mode 100644 index 0000000..552d5d6 --- /dev/null +++ b/src/sensorClass.cpp @@ -0,0 +1,48 @@ +/** + * This class file is used to create flex sensor objects that handle all reading + * and filtering of each sensor signal. +*/ +#include +#include +#include +#include "SensorClass.h" +#include "Config.h" + + +/** + * Constructor + * @param pin the pin to which the flex sensor is connected to. + * @param ads the ADC breakout board object which the flexsensor is connected to. +*/ +sensor::sensor(int pin, ADS1118 ads){ + m_pin = pin; + ads1118 = ads; +} + +/** + * Performs a measurement of the sensor and adds it to the median filter. + * @return a float with the median angle value. +*/ +float sensor::getAngle(){ + float angle = getResistance(m_pin) * 2.6716/1000 - 30.2045 - 60; //From Matlab calibration + + m_f.addSample(angle); + + return m_f.getMedian(); +} + +/** + * Helper method for getAngle(). + * Takes a reading at the voltage divider and returns a resistance. + * @param pin the pin to take a reading from. + * @return a float with the resistance of the flex sensor. +*/ +float sensor::getResistance(int pin){ + float Vflex; + + Vflex = ads1118.getMilliVolts(pin) / 1000.0; + + float Rflex = R_DIV * (VCC / Vflex - 1.0); + return Rflex; +} + diff --git a/src/sensorClass.h b/src/sensorClass.h new file mode 100644 index 0000000..c3e9a15 --- /dev/null +++ b/src/sensorClass.h @@ -0,0 +1,28 @@ +/** + * This class file is used to create flex sensor objects that handle all reading + * and filtering of each sensor signal. +*/ +#pragma once + +#include +#include +#include "Config.h" +#include "MedianFilter.h" + +/** + * Main class +*/ +class sensor{ +private: + // Variables and objects + int m_pin; + ADS1118 ads1118(int); + // Number of samples are defined in config.h + MedianFilter m_f; + // Methods + float getResistance(int); +public: + sensor(int pin, ADS1118 ads){}; + // Methods + float getAngle(); +}; \ No newline at end of file diff --git a/src/talkyBoi.h b/src/talkyBoi.h index 6a4bfdc..aab13c7 100644 --- a/src/talkyBoi.h +++ b/src/talkyBoi.h @@ -1,6 +1,12 @@ +/** + * Lägg till kommentarer här om vad talky boi gör :) + */ +#pragma once + #include #include #include +#include "Config.h" //uint8_t broadcastAdress[] = {0x94,0xB9,0x7E,0xE6,0x79,0x9C}; //MAC-adress till den svarta uint8_t broadcastAdress[] = {0x94,0xB9,0x7E,0xE5,0x31,0xD8}; //MAC-adress till den silver @@ -65,7 +71,6 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { Serial.print(recID); Serial.print(","); Serial.println(recID); - } void getMACAdress(){ @@ -73,7 +78,7 @@ void getMACAdress(){ Serial.println(WiFi.macAddress()); } -void init_wifi (){ +void init_wifi(){ WiFi.mode(WIFI_STA); if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); @@ -95,7 +100,7 @@ void init_wifi (){ esp_now_register_recv_cb(OnDataRecv); } -void send (float sendID, float thumbIP, float thumbMCP, float finger1PIP, float finger1MCP, float finger2PIP, float finger2MCP, float finger3PIP, float finger3MCP, float finger4PIP, float finger4MCP){ +void send(float sendID, float thumbIP, float thumbMCP, float finger1PIP, float finger1MCP, float finger2PIP, float finger2MCP, float finger3PIP, float finger3MCP, float finger4PIP, float finger4MCP){ uint8_t broadcastAdress[] = {0x94,0xB9,0x7E,0xE5,0x31,0xD8}; msg_to_send.sendID = sendID; msg_to_send.thumbIP = thumbIP; @@ -111,12 +116,14 @@ void send (float sendID, float thumbIP, float thumbMCP, float finger1PIP, float esp_err_t result = esp_now_send(broadcastAdress, (uint8_t *) &msg_to_send, sizeof(msg_to_send)); } + void sendToModel(int sendID, float thumbIP, float thumbMCP){ Serial.println("Send to model : "); uint8_t broadcastAdress[] = {0X7C,0X9E,0XBD,0X61,0X58,0XF4}; msg_to_send.sendID = sendID; esp_err_t result = esp_now_send(broadcastAdress,(uint8_t *) &msg_to_send, sizeof(msg_to_send)); } -void recieve () { + +void recieve() { } \ No newline at end of file