diff --git a/Core/Inc/PSE_unit.h b/Core/Inc/PSE_unit.h index 61b4989..5e192b2 100644 --- a/Core/Inc/PSE_unit.h +++ b/Core/Inc/PSE_unit.h @@ -22,6 +22,7 @@ typedef enum {PORT_X, PORT_Y, PORT_Z, PORT_E} pse_ports; typedef struct{ uint32_t step_itvl; uint8_t step_max; + uint32_t tick_counter; GPIO_TypeDef* EN_GPIO_Port; uint16_t EN_GPIO_Pin; diff --git a/Core/Inc/pse_stepper_planer.h b/Core/Inc/pse_stepper_planer.h index d9227c2..28c479c 100644 --- a/Core/Inc/pse_stepper_planer.h +++ b/Core/Inc/pse_stepper_planer.h @@ -10,11 +10,18 @@ #include "PSE_unit.h" +#define PSE_STEPPER_INTERRUPT_RATE 20000 // 64Mhz (Presc 64, count 50) : 20kHz +#define PSE_STEPPER_SCREW_PITCH 1000 // M6 : 1000µm / rotation +#define PSE_STEPPER_STEPS_PER_ROTATION 200 // NEMA17 without microstepping 200 steps/rotation + void pse_sp_start_axis(pse_stepper_conf* conf); void pse_sp_stop_axis(pse_stepper_conf* conf); void pse_sp_set_dir(pse_stepper_conf* conf, int dir); void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num); -unsigned int get_tick_counter(); +void pse_sp_start_all(pse_unit* units, int unit_num); +void pse_sp_stop_all(pse_unit* units, int unit_num); + +void pse_stepper_planer_compute_sps(pse_unit* unit); #endif /* INC_PSE_STEPPER_PLANER_H_ */ diff --git a/Core/Src/PSE_unit.c b/Core/Src/PSE_unit.c index f4c5f90..e0ff0c5 100644 --- a/Core/Src/PSE_unit.c +++ b/Core/Src/PSE_unit.c @@ -19,5 +19,9 @@ void load_units(pse_unit* units, pse_syringe* syringes, pse_stepper_conf* steppe .syringe = &syringes[i], .stepper_conf = &stepper_confs[i], }; + syringes[i] = (pse_syringe){ + .name = "Test", + .diameter = 26700, + }; } } diff --git a/Core/Src/PSE_unit_edit_screen.c b/Core/Src/PSE_unit_edit_screen.c index 4c585d9..e2ca529 100644 --- a/Core/Src/PSE_unit_edit_screen.c +++ b/Core/Src/PSE_unit_edit_screen.c @@ -19,12 +19,17 @@ static lv_obj_t* parent_screen; static lv_obj_t* this_screen; +static pse_unit* c_pse_unit; static void back_button_handler(lv_event_t * e){ lv_event_code_t code = lv_event_get_code(e); if(code == LV_EVENT_CLICKED) { // Save + // update stepper pwm generation + pse_stepper_planer_compute_sps(c_pse_unit); + + // go back to the main menu lv_obj_t* curr_scr = lv_scr_act(); lv_scr_load(parent_screen); lv_obj_del(curr_scr); @@ -192,6 +197,8 @@ void PSE_unit_edit_screen_Gen(lv_obj_t* parent, pse_unit* unit){ lv_obj_t* scr = lv_obj_create(NULL); this_screen = scr; + c_pse_unit = unit; + // create the top menu on the top 20% lv_obj_t* top_menu = lv_obj_create(scr); lv_obj_set_align(top_menu, LV_ALIGN_TOP_LEFT); diff --git a/Core/Src/home_screen.c b/Core/Src/home_screen.c index e9e3411..6428e80 100644 --- a/Core/Src/home_screen.c +++ b/Core/Src/home_screen.c @@ -12,6 +12,10 @@ #include "PSE_unit.h" #include "PSE_unit_edit_screen.h" +#include "pse_stepper_planer.h" + +static pse_unit* units; +static int units_num; static void run_handler(lv_event_t * e){ lv_event_code_t code = lv_event_get_code(e); @@ -22,9 +26,11 @@ static void run_handler(lv_event_t * e){ lv_state_t state = lv_obj_get_state(button); if(state & LV_STATE_CHECKED){ lv_label_set_text(label, LV_SYMBOL_PAUSE); + pse_sp_start_all(units, units_num); } else{ lv_label_set_text(label, LV_SYMBOL_PLAY); + pse_sp_stop_all(units, units_num); } } } @@ -107,6 +113,10 @@ void Home_Screen_Gen(pse_unit* pse_units, uint8_t pse_unit_num){ lv_obj_t* scr = lv_obj_create(NULL); screen = scr; + // set global unit variable + units = pse_units; + units_num = pse_unit_num; + // create the top menu on the top 20% lv_obj_t* top_menu = lv_obj_create(scr); lv_obj_set_align(top_menu, LV_ALIGN_TOP_LEFT); diff --git a/Core/Src/pse_stepper_planer.c b/Core/Src/pse_stepper_planer.c index c0cc678..a60b828 100644 --- a/Core/Src/pse_stepper_planer.c +++ b/Core/Src/pse_stepper_planer.c @@ -9,7 +9,7 @@ #include "PSE_unit.h" -unsigned int tick_counter = 0; +#include "pse_stepper_planer.h" void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num){ for(int i = 0; i < units_num; i++){ @@ -17,19 +17,16 @@ void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num){ int state; if(c->step_max){ - state = tick_counter % 2; + c->tick_counter %= 2; + state = c->tick_counter; } else{ - state = (tick_counter % c->step_itvl) == 0; + c->tick_counter %= c->step_itvl; + state = c->tick_counter == 0; } - HAL_GPIO_WritePin(c->STEP_GPIO_Port, c->STEP_GPIO_Pin, tick_counter%2); + c->tick_counter++; + HAL_GPIO_WritePin(c->STEP_GPIO_Port, c->STEP_GPIO_Pin, state); } - - tick_counter++; -} - -unsigned int get_tick_counter(){ - return tick_counter; } void pse_sp_start_axis(pse_stepper_conf* conf){ @@ -42,3 +39,26 @@ void pse_sp_stop_axis(pse_stepper_conf* conf){ void pse_sp_set_dir(pse_stepper_conf* conf, int dir){ HAL_GPIO_WritePin(conf->DIR_GPIO_Port, conf->DIR_GPIO_Pin, dir); } + + +void pse_stepper_planer_compute_sps(pse_unit* unit){ + pse_stepper_conf* c = unit->stepper_conf; + // magic constants are used to approximate pi and other multiplicatives constant as a rationnal + uint64_t numerator = (uint64_t)1 * unit->syringe->diameter * unit->syringe->diameter * PSE_STEPPER_INTERRUPT_RATE * PSE_STEPPER_SCREW_PITCH; + uint64_t denominator = (uint64_t)21220209 * unit->flow * PSE_STEPPER_STEPS_PER_ROTATION; + c->step_itvl = numerator / denominator; +} + + +void pse_sp_start_all(pse_unit* units, int units_num){ + for(int i = 0; i < units_num; i++){ + if(units[i].enabled) + pse_sp_start_axis(units[i].stepper_conf); + } +} +void pse_sp_stop_all(pse_unit* units, int units_num){ + for(int i = 0; i < units_num; i++){ + if(units[i].enabled) + pse_sp_stop_axis(units[i].stepper_conf); + } +}