<commit_before>#include "motor_driver.h"
#include "gpio_driver.h"
#include "user_config.h"
#include "osapi.h"
#include <stdlib.h>

#define PULSE_FREQUENCY (1 / (RESOLUTION_US * 0.000001))

static volatile unsigned long step_pool = 0;
static volatile signed long stepper_position = 0;

static volatile enum stepper_state motor_state = IDLE;
static volatile enum motor_direction motor_dir = FORWARDS;

static volatile char opcode = ' ';

static volatile int step_rate = 100;
static volatile float rate_counter = 0.0; 
static volatile float rate_incrementor = 0.2;
static const float step_threshold = 1;

void step_driver ( void )
{
	if(motor_state == IDLE) return;
	if(motor_state == DIRECTION_ASSERT)
	{
		//assert the direction for one cycle
		motor_dir == FORWARDS ? eio_high(GPIO_STEP_DIR) : eio_low(GPIO_STEP_DIR);
		motor_state = STEPPING;
	}
	else if(step_pool <= 0)
	{
		motor_state = IDLE;
		eio_low(GPIO_STEP);
		system_os_post(ACK_TASK_PRIO, 0, 0);
	}
	else
	{		
		rate_counter += rate_incrementor;
		//stepping logic
		if(eio_read(GPIO_STEP))
		{
			eio_low(GPIO_STEP);
		}
		else if(rate_counter >= step_threshold)
		{
			rate_counter -= step_threshold;
			if(motor_dir != PAUSED) eio_high(GPIO_STEP) ;
			step_pool--;
			stepper_position += motor_dir;
		}
	}
}

void opcode_move(signed int step_num, unsigned short step_rate, char motor_id)
{
	motor_dir = (step_num >= 0) ? FORWARDS : BACKWARDS;
	eio_low(GPIO_STEP);
	step_pool = motor_dir * step_num;
	rate_counter = 0.0;
	rate_incrementor = calculate_step_incrementor(step_rate);
	opcode = 'M';
	motor_state = DIRECTION_ASSERT;
}

void opcode_goto(signed int step_num, unsigned short step_rate, char motor_id)
{
	motor_dir = (stepper_position <= step_num) ? FORWARDS : BACKWARDS;
	eio_low(GPIO_STEP);
	step_pool = motor_dir * (step_num - stepper_position);
	rate_counter = 0.0;
	rate_incrementor = calculate_step_incrementor(step_rate);
	opcode = 'G';
	motor_state = DIRECTION_ASSERT;
}

void opcode_stop(signed int wait_time, unsigned short precision, char motor_id)
{
	motor_dir = PAUSED;
	eio_low(GPIO_STEP_DIR);
	rate_counter = 0.0;
	rate_incrementor = 1;
	step_pool = abs(wait_time * precision);
	opcode = 'S';
	motor_state = STEPPING;
}

float calculate_step_incrementor(unsigned short input_step_rate)
{
	return input_step_rate / PULSE_FREQUENCY;
}

void driver_logic_task(os_event_t *events)<commit_msg>Stepper motor driver handles paused state before anything else<commit_after>#include "motor_driver.h"
#include "gpio_driver.h"
#include "user_config.h"
#include "osapi.h"
#include <stdlib.h>

#define PULSE_FREQUENCY (1 / (RESOLUTION_US * 0.000001))

static volatile unsigned long step_pool = 0;
static volatile signed long stepper_position = 0;

static volatile enum stepper_state motor_state = IDLE;
static volatile enum motor_direction motor_dir = FORWARDS;

static volatile char opcode = ' ';

static volatile int step_rate = 100;
static volatile float rate_counter = 0.0; 
static volatile float rate_incrementor = 0.2;
static const float step_threshold = 1;

void step_driver ( void )
{
	if((motor_dir == PAUSED) && (motor_state == STEPPING))
	{
		if(step_pool <= 0)
		{
			motor_state = IDLE;
			eio_low(GPIO_STEP);
			system_os_post(ACK_TASK_PRIO, 0, 0);
		}
		else
		{
			step_pool--;
		}
		return;
	}
	switch(motor_state)
	{
		case DIRECTION_ASSERT:
			motor_dir == FORWARDS ? eio_high(GPIO_STEP_DIR) : eio_low(GPIO_STEP_DIR);
			motor_state = STEPPING;
		case IDLE:
			return;
		default:
			if(step_pool <= 0)
			{
				motor_state = IDLE;
				eio_low(GPIO_STEP);
				system_os_post(ACK_TASK_PRIO, 0, 0);
			}
			else
			{
				rate_counter += rate_incrementor;
				//stepping logic
				if(eio_read(GPIO_STEP))
				{
					eio_low(GPIO_STEP);
				}
				else if(rate_counter >= step_threshold)
				{
					rate_counter -= step_threshold;
					eio_high(GPIO_STEP);
					step_pool--;
					stepper_position += motor_dir;
				}
			}
			return;
	}
}

void opcode_move(signed int step_num, unsigned short step_rate, char motor_id)
{
	motor_dir = (step_num >= 0) ? FORWARDS : BACKWARDS;
	eio_low(GPIO_STEP);
	step_pool = motor_dir * step_num;
	rate_counter = 0.0;
	rate_incrementor = calculate_step_incrementor(step_rate);
	opcode = 'M';
	motor_state = DIRECTION_ASSERT;
}

void opcode_goto(signed int step_num, unsigned short step_rate, char motor_id)
{
	motor_dir = (stepper_position <= step_num) ? FORWARDS : BACKWARDS;
	eio_low(GPIO_STEP);
	step_pool = motor_dir * (step_num - stepper_position);
	rate_counter = 0.0;
	rate_incrementor = calculate_step_incrementor(step_rate);
	opcode = 'G';
	motor_state = DIRECTION_ASSERT;
}

void opcode_stop(signed int wait_time, unsigned short precision, char motor_id)
{
	motor_dir = PAUSED;
	eio_low(GPIO_STEP_DIR);
	rate_counter = 0.0;
	rate_incrementor = 1;
	step_pool = abs(wait_time) * precision;
	opcode = 'S';
	motor_state = STEPPING;
}

float calculate_step_incrementor(unsigned short input_step_rate)
{
	return input_step_rate / PULSE_FREQUENCY;
}

void driver_logic_task(os_event_t *events)
{'fraction_non_alphanumeric': 0.10131631431990427, 'fraction_numerical': 0.012165935380933387, 'mean_word_length': 4.744558991981672, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3701982', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1921, 'n_words': 408}