Fix Bug 'weird axis movement at reset'
- guide-stepper:
    - add queue to send commands to stepper-ctl task
    - add function that tells stepper task to move to zero
    - increase travel length full axis length 110
- control: move guide to zero at reset
- Cmake: enable colored output
			
			
This commit is contained in:
		
							parent
							
								
									c42f5d5d7a
								
							
						
					
					
						commit
						989f9cce13
					
				@ -14,3 +14,6 @@ idf_component_register(
 | 
				
			|||||||
    INCLUDE_DIRS 
 | 
					    INCLUDE_DIRS 
 | 
				
			||||||
        "."
 | 
					        "."
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# colored build output (errors, warnings...)
 | 
				
			||||||
 | 
					idf_build_set_property(COMPILE_OPTIONS "-fdiagnostics-color=always" APPEND)
 | 
				
			||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
#include "control.hpp"
 | 
					#include "control.hpp"
 | 
				
			||||||
#include "encoder.hpp"
 | 
					#include "encoder.hpp"
 | 
				
			||||||
 | 
					#include "guide-stepper.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//====================
 | 
					//====================
 | 
				
			||||||
@ -158,6 +159,7 @@ void task_control(void *pvParameter)
 | 
				
			|||||||
        if (SW_RESET.risingEdge) {
 | 
					        if (SW_RESET.risingEdge) {
 | 
				
			||||||
            //dont reset when press used for stopping pending auto-cut
 | 
					            //dont reset when press used for stopping pending auto-cut
 | 
				
			||||||
            if (controlState != systemState_t::AUTO_CUT_WAITING) {
 | 
					            if (controlState != systemState_t::AUTO_CUT_WAITING) {
 | 
				
			||||||
 | 
					                guide_moveToZero();
 | 
				
			||||||
                encoder_reset();
 | 
					                encoder_reset();
 | 
				
			||||||
                lengthNow = 0;
 | 
					                lengthNow = 0;
 | 
				
			||||||
                buzzer.beep(1, 700, 100);
 | 
					                buzzer.beep(1, 700, 100);
 | 
				
			||||||
@ -363,6 +365,7 @@ void task_control(void *pvParameter)
 | 
				
			|||||||
                    //TODO stop if start buttons released?
 | 
					                    //TODO stop if start buttons released?
 | 
				
			||||||
                    changeState(systemState_t::COUNTING);
 | 
					                    changeState(systemState_t::COUNTING);
 | 
				
			||||||
                    //TODO reset automatically or wait for manual reset?
 | 
					                    //TODO reset automatically or wait for manual reset?
 | 
				
			||||||
 | 
					                    guide_moveToZero();
 | 
				
			||||||
                    encoder_reset();
 | 
					                    encoder_reset();
 | 
				
			||||||
                    lengthNow = 0;
 | 
					                    lengthNow = 0;
 | 
				
			||||||
                    buzzer.beep(1, 700, 100);
 | 
					                    buzzer.beep(1, 700, 100);
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@ extern "C"
 | 
				
			|||||||
#include "freertos/task.h"
 | 
					#include "freertos/task.h"
 | 
				
			||||||
#include "esp_log.h"
 | 
					#include "esp_log.h"
 | 
				
			||||||
#include "driver/adc.h"
 | 
					#include "driver/adc.h"
 | 
				
			||||||
 | 
					#include "freertos/queue.h"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "stepper.hpp"
 | 
					#include "stepper.hpp"
 | 
				
			||||||
@ -23,7 +24,7 @@ extern "C"
 | 
				
			|||||||
#define STEPPER_TEST_TRAVEL 65     //mm
 | 
					#define STEPPER_TEST_TRAVEL 65     //mm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define MIN_MM 0
 | 
					#define MIN_MM 0
 | 
				
			||||||
#define MAX_MM 102
 | 
					#define MAX_MM 103
 | 
				
			||||||
#define POS_MAX_STEPS MAX_MM * STEPPER_STEPS_PER_MM
 | 
					#define POS_MAX_STEPS MAX_MM * STEPPER_STEPS_PER_MM
 | 
				
			||||||
#define POS_MIN_STEPS MIN_MM * STEPPER_STEPS_PER_MM
 | 
					#define POS_MIN_STEPS MIN_MM * STEPPER_STEPS_PER_MM
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -47,11 +48,25 @@ static const char *TAG = "stepper-ctrl"; //tag for logging
 | 
				
			|||||||
static bool stepp_direction = true;
 | 
					static bool stepp_direction = true;
 | 
				
			||||||
static uint32_t posNow = 0;
 | 
					static uint32_t posNow = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// queue for sending commands to task handling guide movement
 | 
				
			||||||
 | 
					static QueueHandle_t queue_commandsGuideTask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//----------------------
 | 
					//----------------------
 | 
				
			||||||
//----- functions ------
 | 
					//----- functions ------
 | 
				
			||||||
//----------------------
 | 
					//----------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//==========================
 | 
				
			||||||
 | 
					//==== guide_moveToZero ====
 | 
				
			||||||
 | 
					//==========================
 | 
				
			||||||
 | 
					//tell stepper-control task to move cable guide to zero position
 | 
				
			||||||
 | 
					void guide_moveToZero(){
 | 
				
			||||||
 | 
					    bool valueToSend = true; // or false
 | 
				
			||||||
 | 
					    xQueueSend(queue_commandsGuideTask, &valueToSend, portMAX_DELAY);
 | 
				
			||||||
 | 
					    ESP_LOGI(TAG, "sending command to stepper_ctl task via queue");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//move axis certain Steps (relative) between left and right or reverse when negative
 | 
					//move axis certain Steps (relative) between left and right or reverse when negative
 | 
				
			||||||
void travelSteps(int stepsTarget){
 | 
					void travelSteps(int stepsTarget){
 | 
				
			||||||
	//TODO simplify this function, one simple calculation of new position?
 | 
						//TODO simplify this function, one simple calculation of new position?
 | 
				
			||||||
@ -117,6 +132,9 @@ void init_stepper() {
 | 
				
			|||||||
	//TODO unnecessary wrapper?
 | 
						//TODO unnecessary wrapper?
 | 
				
			||||||
    ESP_LOGW(TAG, "initializing stepper...");
 | 
					    ESP_LOGW(TAG, "initializing stepper...");
 | 
				
			||||||
	stepper_init();
 | 
						stepper_init();
 | 
				
			||||||
 | 
					    // create queue for sending commands to task handling guide movement
 | 
				
			||||||
 | 
					    // currently length 1 and only one command possible, thus bool
 | 
				
			||||||
 | 
					    queue_commandsGuideTask = xQueueCreate(1, sizeof(bool));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -131,9 +149,9 @@ void updateSpeedFromAdc() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//----------------------------
 | 
					//============================
 | 
				
			||||||
//---- TASK stepper-test -----
 | 
					//==== TASK stepper=test =====
 | 
				
			||||||
//----------------------------
 | 
					//============================
 | 
				
			||||||
#ifndef STEPPER_SIMULATE_ENCODER
 | 
					#ifndef STEPPER_SIMULATE_ENCODER
 | 
				
			||||||
void task_stepper_test(void *pvParameter)
 | 
					void task_stepper_test(void *pvParameter)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@ -203,9 +221,9 @@ void task_stepper_test(void *pvParameter)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//----------------------------
 | 
					//============================
 | 
				
			||||||
//----- TASK stepper-ctl -----
 | 
					//===== TASK stepper=ctl =====
 | 
				
			||||||
//----------------------------
 | 
					//============================
 | 
				
			||||||
#ifdef STEPPER_SIMULATE_ENCODER
 | 
					#ifdef STEPPER_SIMULATE_ENCODER
 | 
				
			||||||
void task_stepper_test(void *pvParameter)
 | 
					void task_stepper_test(void *pvParameter)
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
@ -240,8 +258,31 @@ void task_stepper_ctl(void *pvParameter)
 | 
				
			|||||||
        encStepsNow = encoder_getSteps();
 | 
					        encStepsNow = encoder_getSteps();
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // move to zero if command received via queue
 | 
				
			||||||
 | 
					        bool receivedValue;
 | 
				
			||||||
 | 
					        if (xQueueReceive(queue_commandsGuideTask, &receivedValue, 0) == pdTRUE)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            // Process the received value
 | 
				
			||||||
 | 
					            // TODO support other commands (currently only move to zero possible)
 | 
				
			||||||
 | 
					            ESP_LOGW(TAG, "task: move-to-zero command received via queue, starting move, waiting until position reached");
 | 
				
			||||||
 | 
					            stepper_setTargetPosMm(0);
 | 
				
			||||||
 | 
					            stepper_waitForStop();
 | 
				
			||||||
 | 
					            //reset variables -> start tracking cable movement starting from position zero
 | 
				
			||||||
 | 
					            // ensure stepsDelta is 0
 | 
				
			||||||
 | 
					            encStepsNow = encoder_getSteps();
 | 
				
			||||||
 | 
					            encStepsPrev = encStepsNow;
 | 
				
			||||||
 | 
					            travelStepsPartial = 0;
 | 
				
			||||||
 | 
					            // set locally stored axis position to 0 (used for calculating the target axis coordinate)
 | 
				
			||||||
 | 
					            posNow = 0;
 | 
				
			||||||
 | 
					            ESP_LOGW(TAG, "at position 0, resuming normal cable guiding operation");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //calculate change
 | 
					        //calculate change
 | 
				
			||||||
        encStepsDelta = encStepsNow - encStepsPrev; //FIXME MAJOR BUG: when resetting encoder/length in control task, diff will be huge!
 | 
					        encStepsDelta = encStepsNow - encStepsPrev;
 | 
				
			||||||
 | 
					        // check if reset happend without moving to zero before - resulting in huge diff
 | 
				
			||||||
 | 
					        if (encStepsDelta != 0 && encStepsNow == 0){  // this should not happen and causes weird movement
 | 
				
			||||||
 | 
					            ESP_LOGE(TAG, "encoder steps changed to 0 (reset) without previous moveToZero() call, resulting in stepsDelta=%d", encStepsDelta);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //read potentiometer and normalize (0-1) to get a variable for testing
 | 
					        //read potentiometer and normalize (0-1) to get a variable for testing
 | 
				
			||||||
        potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
 | 
					        potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
 | 
				
			||||||
@ -258,8 +299,8 @@ void task_stepper_ctl(void *pvParameter)
 | 
				
			|||||||
        //move axis when ready to move at least 1 step
 | 
					        //move axis when ready to move at least 1 step
 | 
				
			||||||
        if (abs(travelStepsFull) > 1){
 | 
					        if (abs(travelStepsFull) > 1){
 | 
				
			||||||
            travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration
 | 
					            travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration
 | 
				
			||||||
            ESP_LOGD(TAG, "cablelen=%.2lf, turns=%.2lf, travelMm=%.3lf, travelStepsExact: %.3lf, travelStepsFull=%d, partialStep=%.3lf", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial);
 | 
					            ESP_LOGI(TAG, "cablelen=%.2lf, turns=%.2lf, travelMm=%.3lf, travelStepsExact: %.3lf, travelStepsFull=%d, partialStep=%.3lf", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial);
 | 
				
			||||||
            ESP_LOGI(TAG, "MOVING %d steps", travelStepsFull);
 | 
					            ESP_LOGD(TAG, "MOVING %d steps", travelStepsFull);
 | 
				
			||||||
            travelSteps(travelStepsExact);
 | 
					            travelSteps(travelStepsExact);
 | 
				
			||||||
            encStepsPrev = encStepsNow; //update previous length
 | 
					            encStepsPrev = encStepsNow; //update previous length
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -10,3 +10,7 @@ void task_stepper_test(void *pvParameter);
 | 
				
			|||||||
//task that initializes and controls the stepper motor
 | 
					//task that initializes and controls the stepper motor
 | 
				
			||||||
//  - moves stepper according to encoder movement
 | 
					//  - moves stepper according to encoder movement
 | 
				
			||||||
void task_stepper_ctl(void *pvParameter);
 | 
					void task_stepper_ctl(void *pvParameter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//tell stepper-control task to move cable guide to zero position
 | 
				
			||||||
 | 
					void guide_moveToZero();
 | 
				
			||||||
@ -88,7 +88,8 @@ extern "C" void app_main()
 | 
				
			|||||||
    esp_log_level_set("buzzer", ESP_LOG_ERROR);
 | 
					    esp_log_level_set("buzzer", ESP_LOG_ERROR);
 | 
				
			||||||
    esp_log_level_set("switches-analog", ESP_LOG_WARN);
 | 
					    esp_log_level_set("switches-analog", ESP_LOG_WARN);
 | 
				
			||||||
    esp_log_level_set("control", ESP_LOG_INFO);
 | 
					    esp_log_level_set("control", ESP_LOG_INFO);
 | 
				
			||||||
    esp_log_level_set("stepper", ESP_LOG_DEBUG);
 | 
					    esp_log_level_set("stepper-driver", ESP_LOG_WARN);
 | 
				
			||||||
 | 
					    esp_log_level_set("stepper-ctrl", ESP_LOG_INFO);
 | 
				
			||||||
    esp_log_level_set("Dendostepper", ESP_LOG_WARN); //stepper lib
 | 
					    esp_log_level_set("Dendostepper", ESP_LOG_WARN); //stepper lib
 | 
				
			||||||
    esp_log_level_set("calc", ESP_LOG_WARN); //stepper lib
 | 
					    esp_log_level_set("calc", ESP_LOG_WARN); //stepper lib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user