From 135e6a18a6da28c96ddc25a6c36fd5bb5c0ad847 Mon Sep 17 00:00:00 2001 From: Mac D Date: Tue, 15 Mar 2022 14:34:13 +0100 Subject: [PATCH 1/4] Create branch for next project version --- src/Filter.h | 90 ------------------------ src/FilterTest.cpp | 47 ------------- src/config.h | 18 +++++ src/flexsensorClass.cpp | 37 ---------- src/readFingers.h | 31 -------- src/sensorClass.cpp | 43 +++++++++++ src/{flexsensorClass.h => sensorClass.h} | 18 +++-- src/talkyBoi.h | 10 +-- 8 files changed, 75 insertions(+), 219 deletions(-) delete mode 100644 src/Filter.h delete mode 100644 src/FilterTest.cpp create mode 100644 src/config.h delete mode 100644 src/flexsensorClass.cpp delete mode 100644 src/readFingers.h create mode 100644 src/sensorClass.cpp rename src/{flexsensorClass.h => sensorClass.h} (61%) 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 -#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..9675ffd --- /dev/null +++ b/src/sensorClass.cpp @@ -0,0 +1,43 @@ +/* + This class file is used to create flex sensor objects that handle all reading + and filtering of each sensor signal. +*/ +#include "sensorClass.h" +#include +#include +#include +#include "config.h" + +/* + Constructor +*/ +// 0<=type<6 -> on esp board, 6= on multiplexer +sensor::sensor(int pin, ADS1118 ads){ + m_pin = pin; + ads1118 = ads; +} + +/* + Performs a measurement of the sensor and adds it to the median filter. + Returns the median sensor reading 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(). +*/ +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/flexsensorClass.h b/src/sensorClass.h similarity index 61% rename from src/flexsensorClass.h rename to src/sensorClass.h index 2e951e4..2bc6f62 100644 --- a/src/flexsensorClass.h +++ b/src/sensorClass.h @@ -5,25 +5,23 @@ #pragma once #include +#include #include "config.h" #include "MedianFilter.h" /* Main class */ -class flexsensor{ - // Variables +class sensor{ private: + // Variables and objects int m_pin; - String m_location; + ADS1118 ads1118(int); MedianFilter m_f; - + // Methods + float getResistance(int); public: - flexsensor(int pin, String location){}; + sensor(int pin, ADS1118 ads){}; // Methods - // ... Gets - float getMedian(); - String getlocation(); - // ... Sets - void read(); + float getAngle(); }; \ No newline at end of file diff --git a/src/talkyBoi.h b/src/talkyBoi.h index 6a4bfdc..7a8a4b3 100644 --- a/src/talkyBoi.h +++ b/src/talkyBoi.h @@ -1,6 +1,7 @@ #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 +66,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 +73,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 +95,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 +111,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 From 104cf860afcbed8a30f4467595e0fe35af3cc481 Mon Sep 17 00:00:00 2001 From: Mac D Date: Wed, 16 Mar 2022 09:09:24 +0100 Subject: [PATCH 2/4] Change formatting --- src/config.h | 8 ++++---- src/sensorClass.cpp | 15 ++++++++------- src/sensorClass.h | 9 +++++---- src/talkyBoi.h | 7 ++++++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/config.h b/src/config.h index ee2f247..43669f7 100644 --- a/src/config.h +++ b/src/config.h @@ -1,6 +1,6 @@ -/* - Configuration file for the entire project. - Define and change all constant variables here. +/** + * Configuration file for the entire project. + * Define and change all constant variables here! */ #pragma once @@ -15,4 +15,4 @@ const float R_DIV = 100000.0; // Resistor used to create a voltage divider const int SAMPLES = 20; // Pin to the push button that turns the controller on -#define interuptPin 16 \ No newline at end of file +#define INTERUPT_PIN 16 \ No newline at end of file diff --git a/src/sensorClass.cpp b/src/sensorClass.cpp index 9675ffd..f80ee5a 100644 --- a/src/sensorClass.cpp +++ b/src/sensorClass.cpp @@ -1,17 +1,17 @@ -/* - This class file is used to create flex sensor objects that handle all reading - and filtering of each sensor signal. +/** + * This class file is used to create flex sensor objects that handle all reading + * and filtering of each sensor signal. */ -#include "sensorClass.h" #include #include #include -#include "config.h" +#include "SensorClass.h" +#include "Config.h" + /* Constructor */ -// 0<=type<6 -> on esp board, 6= on multiplexer sensor::sensor(int pin, ADS1118 ads){ m_pin = pin; ads1118 = ads; @@ -19,7 +19,7 @@ sensor::sensor(int pin, ADS1118 ads){ /* Performs a measurement of the sensor and adds it to the median filter. - Returns the median sensor reading value. + Returns the median angle value. */ float sensor::getAngle(){ float angle = getResistance(m_pin) * 2.6716/1000 - 30.2045 - 60; //From Matlab calibration @@ -31,6 +31,7 @@ float sensor::getAngle(){ /* Helper method for getAngle(). + Takes a reading at the voltage divider and returns a resistance. */ float sensor::getResistance(int pin){ float Vflex; diff --git a/src/sensorClass.h b/src/sensorClass.h index 2bc6f62..00cf161 100644 --- a/src/sensorClass.h +++ b/src/sensorClass.h @@ -1,12 +1,12 @@ -/* - This class file is used to create flex sensor objects that handle all reading - and filtering of each sensor signal. +/** + * 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 "Config.h" #include "MedianFilter.h" /* @@ -17,6 +17,7 @@ class sensor{ // Variables and objects int m_pin; ADS1118 ads1118(int); + // Number of samples are defined in config.h MedianFilter m_f; // Methods float getResistance(int); diff --git a/src/talkyBoi.h b/src/talkyBoi.h index 7a8a4b3..c3c4a2d 100644 --- a/src/talkyBoi.h +++ b/src/talkyBoi.h @@ -1,7 +1,12 @@ +/** + * + */ +#pragma once + #include #include #include -#include "config.h" +#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 From 3992ed2e88b17e6001c99d06d65a5da71c6eb961 Mon Sep 17 00:00:00 2001 From: Mac D Date: Wed, 16 Mar 2022 09:17:12 +0100 Subject: [PATCH 3/4] Add comment blocks --- src/main.cpp | 73 +++++++++++++++++++-------------------------- src/sensorClass.cpp | 12 +++++--- src/talkyBoi.h | 2 +- 3 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d816d39..8a5da30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,17 @@ #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}; @@ -24,6 +21,9 @@ int o = 0; int buttonRun = 0; int state = 0; +/* + +*/ void initBoard(){ ads1118.begin(); // Initialize board ads1118.setSamplingRate(ads1118.RATE_860SPS); // highest sampling rate possible @@ -31,13 +31,12 @@ void initBoard(){ } 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); - + pinMode(f2PIP_Pin, INPUT); // maybe change later! + pinMode(f2MCP_Pin, INPUT); + pinMode(f3PIP_Pin, INPUT); + pinMode(f3MCP_Pin, INPUT); + pinMode(f4PIP_Pin, INPUT); + pinMode(f4MCP_Pin, INPUT); } void interuptFunc(){ @@ -60,39 +59,29 @@ 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 + initAnalogPin(); // Initiate analog pins on ESP + //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/sensorClass.cpp b/src/sensorClass.cpp index f80ee5a..b3b20e9 100644 --- a/src/sensorClass.cpp +++ b/src/sensorClass.cpp @@ -9,17 +9,19 @@ #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. - Returns the median angle value. + @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 @@ -29,9 +31,11 @@ float sensor::getAngle(){ 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; diff --git a/src/talkyBoi.h b/src/talkyBoi.h index c3c4a2d..aab13c7 100644 --- a/src/talkyBoi.h +++ b/src/talkyBoi.h @@ -1,5 +1,5 @@ /** - * + * Lägg till kommentarer här om vad talky boi gör :) */ #pragma once From 2ef0b826e5deaa9cffad5ed78abc282a932e4466 Mon Sep 17 00:00:00 2001 From: Mac D Date: Wed, 16 Mar 2022 09:30:59 +0100 Subject: [PATCH 4/4] Clean comments and code --- src/main.cpp | 30 ++++++------------------------ src/sensorClass.cpp | 18 +++++++++--------- src/sensorClass.h | 4 ++-- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8a5da30..51c0b8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,13 +16,12 @@ const int pinList[] = {thumbIP_Pin, thumbMCP_Pin, f1PIP_Pin, f1MCP_Pin, f2PIP_Pi 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 @@ -30,15 +29,9 @@ void initBoard(){ ads1118.setFullScaleRange(ads1118.FSR_4096); // 12 bit } -void initAnalogPin(){ - pinMode(f2PIP_Pin, INPUT); // maybe change later! - pinMode(f2MCP_Pin, INPUT); - pinMode(f3PIP_Pin, INPUT); - pinMode(f3MCP_Pin, INPUT); - pinMode(f4PIP_Pin, INPUT); - pinMode(f4MCP_Pin, INPUT); -} - +/** + * Toggles the state of the microcontroller. +*/ void interuptFunc(){ static unsigned long last_interuptTime = 0; unsigned long interupt_time = millis(); @@ -55,31 +48,20 @@ void interuptFunc(){ } - void setup() { Serial.begin(115200); delay(100); 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(); // Initiate analog pins on ESP + initBoard(); // Initiate breakout board //attachInterrupt(17, interuptFunc, HIGH); // interupt for start/stop button } void loop() { - //while(!state){} //Remove this comment to have a butten that locks/unlocks the program when it is run. Needs a debounce for the button before it works. - - for(int i = 0; i