Merge branch 'main' into cutter

This commit is contained in:
jonny_l480
2022-09-12 13:48:35 +02:00
20 changed files with 1274 additions and 165 deletions

View File

@@ -7,6 +7,7 @@ idf_component_register(
"vfd.cpp"
"display.cpp"
"cutter.cpp"
"switchesAnalog.cpp"
INCLUDE_DIRS
"."
)

View File

@@ -3,13 +3,20 @@
//--- inputs ---
//create and configure objects for evaluated switches
gpio_evaluatedSwitch SW_START(GPIO_SW_START, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch SW_RESET(GPIO_SW_RESET, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch SW_SET(GPIO_SW_SET, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch SW_PRESET1(GPIO_SW_PRESET1, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch SW_PRESET2(GPIO_SW_PRESET2, false, true); //pullup false, INVERTED (switch to 3V3, pulldown on pcb soldered)
gpio_evaluatedSwitch SW_PRESET3(GPIO_SW_PRESET3, false, true); //pullup false, INVERTED (switch to 3V3, pulldown on pcb soldered)
//gpio_evaluatedSwitch sw_gpio_39(GPIO_NUM_39, false, true); //pullup false, INVERTED (switch to 3V3, pulldown on pcb soldered)
gpio_evaluatedSwitch sw_gpio_34(GPIO_NUM_34, false, true); //pullup false, INVERTED (switch to 3V3, pulldown on pcb soldered)
gpio_evaluatedSwitch sw_gpio_32(GPIO_NUM_32, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch sw_gpio_33(GPIO_NUM_33, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch sw_gpio_25(GPIO_NUM_25, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch sw_gpio_26(GPIO_NUM_26, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
gpio_evaluatedSwitch sw_gpio_14(GPIO_NUM_14, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
//--- switches connected to 4 sw to analog stripboard ---
//evaluated switches with function to obtain the current input state instead of gpio
gpio_evaluatedSwitch sw_gpio_analog_0(&switchesAnalog_getState_sw0);
gpio_evaluatedSwitch sw_gpio_analog_1(&switchesAnalog_getState_sw1);
gpio_evaluatedSwitch sw_gpio_analog_2(&switchesAnalog_getState_sw2);
gpio_evaluatedSwitch sw_gpio_analog_3(&switchesAnalog_getState_sw3);
//create buzzer object with no gap between beep events
buzzer_t buzzer(GPIO_BUZZER, 0);

View File

@@ -4,6 +4,7 @@ extern "C" {
}
#include "gpio_evaluateSwitch.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
//===================================
@@ -22,18 +23,35 @@ extern "C" {
//==================================
//===== define input gpio pins =====
//==== define analog input pins ====
//==================================
#define GPIO_SW_START GPIO_NUM_26
#define GPIO_SW_RESET GPIO_NUM_25
#define GPIO_SW_SET GPIO_NUM_33
#define GPIO_SW_PRESET1 GPIO_NUM_32
#define GPIO_SW_PRESET2 GPIO_NUM_34
#define GPIO_SW_PRESET3 GPIO_NUM_39
#define GPIO_CUTTER_POS_SW GPIO_NUM_14
#define GPIO_POTI GPIO_NUM_36
#define ADC_CHANNEL_POTI ADC1_CHANNEL_0
#define GPIO_4SW_TO_ANALOG GPIO_NUM_39
#define ADC_CHANNEL_4SW_TO_ANALOG ADC1_CHANNEL_3 //gpio 39
//ADC1_CHANNEL_0 gpio36
//ADC1_CHANNEL_6 gpio_34
//ADC1_CHANNEL_3 gpio_39
//=====================================
//==== assign switches to objects =====
//=====================================
//see config.cpp for available evaluated switch objects
#define SW_START sw_gpio_26
#define SW_RESET sw_gpio_25
#define SW_CUTTER_POS sw_gpio_14
#define SW_SET sw_gpio_analog_0
#define SW_PRESET1 sw_gpio_analog_1
#define SW_PRESET2 sw_gpio_analog_2
#define SW_PRESET3 sw_gpio_analog_3
//unused but already available evaluated inputs
//#define ? sw_gpio_33
//#define ? sw_gpio_32
//#define ? sw_gpio_34
//--------------------------
@@ -72,14 +90,22 @@ extern "C" {
//===== global variables =====
//============================
//create global evaluated switch objects
//--- inputs ---
//create objects for switches at bottom screw temerinals
extern gpio_evaluatedSwitch SW_START;
extern gpio_evaluatedSwitch SW_RESET;
extern gpio_evaluatedSwitch SW_SET;
extern gpio_evaluatedSwitch SW_PRESET1;
extern gpio_evaluatedSwitch SW_PRESET2;
extern gpio_evaluatedSwitch SW_PRESET3;
//--- switches on digital gpio pins ---
//extern gpio_evaluatedSwitch sw_gpio_39;
extern gpio_evaluatedSwitch sw_gpio_34;
extern gpio_evaluatedSwitch sw_gpio_32;
extern gpio_evaluatedSwitch sw_gpio_33;
extern gpio_evaluatedSwitch sw_gpio_25;
extern gpio_evaluatedSwitch sw_gpio_26;
extern gpio_evaluatedSwitch sw_gpio_14;
//--- switches connected to 4-sw-to-analog stripboard ---
extern gpio_evaluatedSwitch sw_gpio_analog_0;
extern gpio_evaluatedSwitch sw_gpio_analog_1;
extern gpio_evaluatedSwitch sw_gpio_analog_2;
extern gpio_evaluatedSwitch sw_gpio_analog_3;
//create global buzzer object
extern buzzer_t buzzer;

View File

@@ -23,27 +23,6 @@ QueueHandle_t init_encoder(rotary_encoder_info_t * info){
}
//=============================
//========= readAdc ===========
//=============================
//function for multisampling an anlog input
int readAdc(adc1_channel_t adc_channel, bool inverted = false) {
//make multiple measurements
int adc_reading = 0;
for (int i = 0; i < 16; i++) {
adc_reading += adc1_get_raw(adc_channel);
}
adc_reading = adc_reading / 16;
//return original or inverted result
if (inverted) {
return 4095 - adc_reading;
} else {
return adc_reading;
}
}
//====================
@@ -228,7 +207,7 @@ void task_control(void *pvParameter)
//set target length to poti position when SET switch is pressed
if (SW_SET.state == true) {
//read adc
potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095
potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095
//scale to target length range
int lengthTargetNew = (float)potiRead / 4095 * 30000;
//apply hysteresis and round to whole meters //TODO optimize this
@@ -341,7 +320,7 @@ void task_control(void *pvParameter)
case MANUAL: //manually control motor via preset buttons + poti
//read poti value
potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095
potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095
//scale poti to speed levels 0-3
uint8_t level = round( (float)potiRead / 4095 * 3 );
//exit manual mode if preset2 released

View File

@@ -18,6 +18,7 @@ extern "C"
#include "config.hpp"
#include "gpio_evaluateSwitch.hpp"
#include "gpio_adc.hpp"
#include "buzzer.hpp"
#include "vfd.hpp"
#include "display.hpp"

View File

@@ -19,7 +19,6 @@ cutter_state_t cutter_state = cutter_state_t::IDLE;
uint32_t timestamp_turnedOn;
uint32_t msTimeout = 3000;
static const char *TAG = "cutter"; //tag for logging
gpio_evaluatedSwitch POSITION_SW(GPIO_CUTTER_POS_SW, true, false); //pullup true, not inverted (switch to GND, internal pullup used)
@@ -39,7 +38,7 @@ void cutter_start(){
//========= stop =========
//========================
void cutter_stop(){
if(cutter_state =! cutter_state_t::IDLE){
if(cutter_state != cutter_state_t::IDLE){
setState(cutter_state_t::CANCELED);
}
//starts motor on state change
@@ -115,10 +114,10 @@ bool checkTimeout(){
//function that handles the cutter logic -> has to be run repeatedly
void cutter_handle(){
//handle evaluated switch (position switch)
POSITION_SW.handle();
SW_CUTTER_POS.handle();
//TODO add custom thresholds once at initialization?
//POSITION_SW.minOnMs = 10;
//POSITION_SW.minOffMs = 10;
//SW_CUTTER_POS.minOnMs = 10;
//SW_CUTTER_POS.minOffMs = 10;
switch(cutter_state){
@@ -131,7 +130,7 @@ void cutter_handle(){
case cutter_state_t::START:
//--- moved away from idle position ---
//if (gpio_get_level(GPIO_CUTTER_POS_SW) == 0){ //contact closed
if (POSITION_SW.state == true) { //contact closed -> not at idle pos
if (SW_CUTTER_POS.state == true) { //contact closed -> not at idle pos
setState(cutter_state_t::CUTTING);
}
//--- timeout ---
@@ -145,7 +144,7 @@ void cutter_handle(){
//--- idle position reached ---
//if (gpio_get_level(GPIO_CUTTER_POS_SW) == 1){ //contact not closed
//TODO: add min on duration
if (POSITION_SW.state == false) { //contact open -> at idle pos
if (SW_CUTTER_POS.state == false) { //contact open -> at idle pos
setState(cutter_state_t::IDLE);
}
//--- timeout ---

View File

@@ -14,6 +14,8 @@ extern "C"
#include "control.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
//=================================
//=========== functions ===========
@@ -84,6 +86,7 @@ extern "C" void app_main()
//define loglevel
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("buzzer", ESP_LOG_ERROR);
esp_log_level_set("switches-analog", ESP_LOG_WARN);
esp_log_level_set("control", ESP_LOG_INFO);
//create task for controlling the machine
@@ -93,4 +96,5 @@ extern "C" void app_main()
//beep at startup
buzzer.beep(3, 70, 50);
}

103
main/switchesAnalog.cpp Normal file
View File

@@ -0,0 +1,103 @@
#include "switchesAnalog.hpp"
#define CHECK_BIT(var,pos) (((var)>>(pos)) & 1) //TODO duplicate code: same macro already used in vfd.cpp
//=====================
//===== Variables =====
//=====================
static const char *TAG = "switches-analog"; //tag for logging
int diffMin = 4095;
int adcValue;
int match_index = 0;
//array that describes voltages for all combinations of the 4 inputs
const int lookup_voltages[] = {
//ADC, S3 S2 S1 S0
4095, //0000
3780, //0001
3390, //0010
3040, //0011
2760, //0100
2542, //0101
2395, //0110
2225, //0111
1964, //1000
1845, //1001
1762, //1010
1664, //1011
1573, //1100
1485, //1101
1432, //1110
1363 //1111
};
//===========================
//===== handle function =====
//===========================
//handle demuxing of 4 switches from 1 adc (has to be run repeatedly)
void handle(){
//read current voltage
adcValue = gpio_readAdc(ADC_CHANNEL_4SW_TO_ANALOG);
ESP_LOGI(TAG, "voltage read: %d", adcValue);
//find closest match in lookup table
diffMin = 4095; //reset diffMin each run
for (int i=0; i<16; i++){
int diff = fabs(adcValue - lookup_voltages[i]);
if (diff < diffMin){
diffMin = diff;
match_index = i;
}
}
//get bool values for each input from matched index
bool s0 = CHECK_BIT(match_index, 0);
bool s1 = CHECK_BIT(match_index, 1);
bool s2 = CHECK_BIT(match_index, 2);
bool s3 = CHECK_BIT(match_index, 3);
//bool s1 = ((match_index & 0b1000) == 0b1000);
//bool s2 = ((match_index & 0b0100) == 0b0100);
//bool s3 = ((match_index & 0b0010) == 0b0010);
//bool s4 = ((match_index & 0b0001) == 0b0001);
//log results
ESP_LOGI(TAG, "adcRead: %d, closest-match: %d, diff: %d, index: %d, switches: %d%d%d%d",
adcValue, lookup_voltages[match_index], diffMin, match_index, (int)s3, (int)s2, (int)s1, (int)s0);
}
//====================
//===== getState =====
//====================
//get state of certain switch (0-3)
bool switchesAnalog_getState(int swNumber){
//run handle function to obtain all current input states
handle();
//get relevant bit
bool state = CHECK_BIT(match_index, swNumber);
ESP_LOGI(TAG, "returned state of switch No. %d = %i", swNumber, (int)state);
return state;
}
bool switchesAnalog_getState_sw0(){
return switchesAnalog_getState(0);
}
bool switchesAnalog_getState_sw1(){
return switchesAnalog_getState(1);
}
bool switchesAnalog_getState_sw2(){
return switchesAnalog_getState(2);
}
bool switchesAnalog_getState_sw3(){
return switchesAnalog_getState(3);
}

21
main/switchesAnalog.hpp Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
extern "C"
{
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include "esp_log.h"
#include "driver/adc.h"
#include <math.h>
}
#include "config.hpp"
#include "gpio_adc.hpp"
//get current state of certain switch
bool switchesAnalog_getState(int swNumber);
bool switchesAnalog_getState_sw0();
bool switchesAnalog_getState_sw1();
bool switchesAnalog_getState_sw2();
bool switchesAnalog_getState_sw3();