diff --git a/Inc/stepper.h b/Inc/stepper.h index 364cc59..d4abf53 100644 --- a/Inc/stepper.h +++ b/Inc/stepper.h @@ -1,4 +1,5 @@ #include "stm32g0b1xx.h" +#include typedef struct { GPIO_TypeDef* en_port; @@ -11,13 +12,15 @@ typedef struct { uint8_t home_pin; uint8_t homed:1; uint8_t trigger_step:1; + int32_t home_position; int32_t pos; + int32_t endless_rot_integrator; int32_t speed; int32_t ramp_to_speed; uint32_t next_step; } stepper_T; -#define STEPPER_MAX_SPEED 600 +#define STEPPER_MAX_SPEED 400 #define STEPPER_STEPS_PER_ROTATION 3200 diff --git a/Makefile b/Makefile index 4270395..1e35bda 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ TARGET = Parafraktor # debug build? DEBUG = 1 # optimization -OPT = -Og +OPT = -O0 ####################################### @@ -38,6 +38,7 @@ BUILD_DIR = build C_SOURCES = \ Src/main.c \ Src/uart_dmx.c \ +Src/address_selector.c \ Src/stepper.c \ Src/stm32g0xx_it.c \ Src/stm32g0xx_hal_msp.c \ diff --git a/Src/main.c b/Src/main.c index 135a44b..e3a1d2a 100644 --- a/Src/main.c +++ b/Src/main.c @@ -30,6 +30,7 @@ #include "uart_dmx.h" #include "stepper.h" +#include "address_selector.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -56,6 +57,8 @@ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); + +uint8_t dmx_address; /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -99,6 +102,9 @@ int main(void) /* USER CODE BEGIN 2 */ init_UART1_dma(); stepper_gpio_init(); + adress_selector_init_gpio(); + dmx_address = read_address(); + Timer1_Init(); /* USER CODE END 2 */ @@ -112,6 +118,7 @@ int main(void) // printf("0x%02X ", rxBuffer[i]); //printf("\n"); SEGGER_SYSVIEW_OnIdle(); + //read_address(); //if(cnt++ >= 256-rxBuffer[1]){ // cnt = 0; // GPIOE->ODR ^= GPIO_ODR_OD2; diff --git a/Src/stepper.c b/Src/stepper.c index b5eedc2..41edd7d 100644 --- a/Src/stepper.c +++ b/Src/stepper.c @@ -6,10 +6,11 @@ #include "SEGGER_SYSVIEW.h" #include "uart_dmx.h" +#include "address_selector.h" stepper_T steppers[] = { - {.en_port=GPIOC, .en_pin=11, .dir_port=GPIOB, .dir_pin=4, .step_port=GPIOE, .step_pin=2, .home_port=GPIOF, .home_pin=3}, - {.en_port=GPIOB, .en_pin=3, .dir_port=GPIOF, .dir_pin=11, .step_port=GPIOF, .step_pin=12, .home_port=GPIOF, .home_pin=4} + {.en_port=GPIOC, .en_pin=11, .dir_port=GPIOB, .dir_pin=4, .step_port=GPIOE, .step_pin=2, .home_port=GPIOF, .home_pin=3, .home_position=1000}, + {.en_port=GPIOB, .en_pin=3, .dir_port=GPIOF, .dir_pin=11, .step_port=GPIOF, .step_pin=12, .home_port=GPIOF, .home_pin=4, .home_position=0} }; void set_pins(stepper_T* stp){ @@ -122,14 +123,52 @@ void do_steps(){ steppers[i].next_step = timer + 1; } - printf("motor[%d]: pos %ld\tspeed %ld\n", i, steppers[i].pos, steppers[i].speed); + printf("motor[%d]: pos %ld\trot: %ld\tspeed %ld\n", i, steppers[i].pos, steppers[i].endless_rot_integrator, steppers[i].speed); } if(steppers[i].homed){ static uint16_t old_ramp_to_speed; old_ramp_to_speed = steppers[i].ramp_to_speed; - int32_t swapped_val = rxBuffer[2*i+1]<<8 | (rxBuffer[2*i+2]&0xFF); + uint8_t raw_endless = rxBuffer[6*i + DMX_ADDRESS + 2]; + int16_t rotation_speed; + + ///* deadzone on top and bottom */ + //if(raw_endless >= 5 && raw_endless <= 250) { + // rotation_speed = raw_endless - 127; + + // /* dead zone in the middle */ + // if(rotation_speed >= 5) + // rotation_speed -= 5; + // else if(rotation_speed <= -5) + // rotation_speed += 5; + // else + // rotation_speed = 0; + //} + //else{ + // rotation_speed = 0; + //} + + ///* handle endless rotation offset and wraparound */ + //if(rotation_speed){ + // steppers[i].endless_rot_integrator += rotation_speed; + // if(steppers[i].endless_rot_integrator >= STEPPER_STEPS_PER_ROTATION) + // { + // steppers[i].endless_rot_integrator -= STEPPER_STEPS_PER_ROTATION; + // steppers[i].pos -= STEPPER_STEPS_PER_ROTATION; + // } + // else if(steppers[i].endless_rot_integrator <= 0) + // { + // steppers[i].endless_rot_integrator += STEPPER_STEPS_PER_ROTATION; + // steppers[i].pos += STEPPER_STEPS_PER_ROTATION; + // } + //} + //else + // steppers[i].endless_rot_integrator = 0; + + + int32_t swapped_val = rxBuffer[6*i + DMX_ADDRESS]<<8 | (rxBuffer[6*i+DMX_ADDRESS+1]&0xFF); + //swapped_val += steppers[i].endless_rot_integrator; int32_t scaled_steps = swapped_val * STEPPER_STEPS_PER_ROTATION / UINT16_MAX; steppers[i].ramp_to_speed = scaled_steps - steppers[i].pos; @@ -138,7 +177,7 @@ void do_steps(){ } else{ if(~(steppers[i].home_port->IDR) & (1 << steppers[i].home_pin)){ - steppers[i].pos = 0; + steppers[i].pos = steppers[i].home_position; steppers[i].homed = 1; steppers[i].speed = 0; steppers[i].ramp_to_speed = 0; diff --git a/compile_flags.txt b/compile_flags.txt index 92d6f97..9edb108 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -12,7 +12,7 @@ -IDrivers/SystemView/Config -IDrivers/SystemView/SEGGER -IDrivers/SystemView/SYSVIEW --Og +-O0 -Wall -fdata-sections -ffunction-sections