Add basic control task without timer (works, but dropped)
basic task for controlling the stepper (no accel / decel etc) works at current speed settings but long moves trigger watchdog so dropped this idea and using timer as before
This commit is contained in:
		
							parent
							
								
									7bde75806c
								
							
						
					
					
						commit
						1e2fa1db8f
					
				@ -188,15 +188,15 @@ void task_stepper_test(void *pvParameter)
 | 
			
		||||
		}
 | 
			
		||||
		if (SW_PRESET1.risingEdge) {
 | 
			
		||||
			buzzer.beep(2, 300, 100);
 | 
			
		||||
			stepper_setTargetSteps(1000);
 | 
			
		||||
			stepperSw_setTargetSteps(1000);
 | 
			
		||||
		}
 | 
			
		||||
		if (SW_PRESET2.risingEdge) {
 | 
			
		||||
			buzzer.beep(1, 500, 100);
 | 
			
		||||
			stepper_setTargetSteps(10000);
 | 
			
		||||
			stepperSw_setTargetSteps(10000);
 | 
			
		||||
		}
 | 
			
		||||
		if (SW_PRESET3.risingEdge) {
 | 
			
		||||
			buzzer.beep(1, 100, 100);
 | 
			
		||||
			stepper_setTargetSteps(60000);
 | 
			
		||||
			stepperSw_setTargetSteps(30000);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,8 @@ extern "C"
 | 
			
		||||
#include "guide-stepper.hpp"
 | 
			
		||||
#include "encoder.hpp"
 | 
			
		||||
 | 
			
		||||
#include "stepper.hpp"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//=================================
 | 
			
		||||
//=========== functions ===========
 | 
			
		||||
@ -92,13 +94,14 @@ extern "C" void app_main()
 | 
			
		||||
 | 
			
		||||
#ifdef STEPPER_TEST
 | 
			
		||||
    //create task for stepper testing
 | 
			
		||||
    xTaskCreate(task_stepper_test, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
 | 
			
		||||
    xTaskCreate(task_stepperCtrlSw, "task stepper control software", configMINIMAL_STACK_SIZE * 3, NULL, configMAX_PRIORITIES - 1, NULL);
 | 
			
		||||
    xTaskCreate(task_stepper_test, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
 | 
			
		||||
#else
 | 
			
		||||
    //create task for controlling the machine
 | 
			
		||||
    xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
 | 
			
		||||
    xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 4, NULL);
 | 
			
		||||
 | 
			
		||||
    //create task for controlling the stepper
 | 
			
		||||
    xTaskCreate(task_stepper_ctl, "task_stepper_ctl", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
 | 
			
		||||
    xTaskCreate(task_stepper_ctl, "task_stepper_ctl", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    //create task for handling the buzzer
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
#include "config.hpp"
 | 
			
		||||
 | 
			
		||||
#include <inttypes.h>
 | 
			
		||||
 | 
			
		||||
//config from config.hpp
 | 
			
		||||
//#define STEPPER_STEP_PIN GPIO_NUM_18    //mos1
 | 
			
		||||
@ -11,6 +12,7 @@
 | 
			
		||||
extern "C" {
 | 
			
		||||
#include "driver/timer.h"
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "esp_log.h"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define TIMER_F 1000000ULL
 | 
			
		||||
@ -24,6 +26,60 @@ bool timerIsRunning = false;
 | 
			
		||||
bool timer_isr(void *arg);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//stepper driver in software for testing (no timer/interrupt):
 | 
			
		||||
uint64_t stepsTarget = 0;
 | 
			
		||||
void task_stepperCtrlSw(void *pvParameter)
 | 
			
		||||
{
 | 
			
		||||
	uint64_t stepsNow = 0;
 | 
			
		||||
	int increment = 1;
 | 
			
		||||
 | 
			
		||||
	while(1){
 | 
			
		||||
		int64_t delta = stepsTarget - stepsNow;
 | 
			
		||||
 | 
			
		||||
		//at position, nothing to do
 | 
			
		||||
		if (delta == 0){
 | 
			
		||||
			ESP_LOGW(TAG, "delta =0 waiting for change");
 | 
			
		||||
			vTaskDelay(300 / portTICK_PERIOD_MS);
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//define direction
 | 
			
		||||
		if (delta > 0){
 | 
			
		||||
			gpio_set_level(STEPPER_DIR_PIN, 0);
 | 
			
		||||
			increment = 1;
 | 
			
		||||
		} else {
 | 
			
		||||
			gpio_set_level(STEPPER_DIR_PIN, 1);
 | 
			
		||||
			increment = -1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//do one step
 | 
			
		||||
		//note: triggers watchdog at long movements
 | 
			
		||||
		stepsNow += increment;
 | 
			
		||||
		gpio_set_level(STEPPER_STEP_PIN, 1);
 | 
			
		||||
		ets_delay_us(30);
 | 
			
		||||
		gpio_set_level(STEPPER_STEP_PIN, 0);
 | 
			
		||||
		ets_delay_us(90); //speed
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void stepperSw_setTargetSteps(uint64_t target){
 | 
			
		||||
	stepsTarget = target;
 | 
			
		||||
	char buffer[20];
 | 
			
		||||
	snprintf(buffer, sizeof(buffer), "%" PRIu64, target);
 | 
			
		||||
	ESP_LOGW(TAG, "set target steps to %s", buffer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	int targetSteps;        // Target step count
 | 
			
		||||
	int currentSteps;       // Current step count
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,10 @@ void stepper_setTargetSteps(int steps);
 | 
			
		||||
 | 
			
		||||
void stepper_toggleDirection();
 | 
			
		||||
 | 
			
		||||
//control stepper without timer (software)
 | 
			
		||||
void task_stepperCtrlSw(void *pvParameter);
 | 
			
		||||
void stepperSw_setTargetSteps(uint64_t target);
 | 
			
		||||
 | 
			
		||||
//typedef struct
 | 
			
		||||
//{
 | 
			
		||||
//    uint8_t stepPin;           /** step signal pin */
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user