From 1b3e25178343e489c15b2258288e1c6797c17e5b Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Thu, 9 Mar 2023 00:05:12 +0000 Subject: [PATCH] Support 4 motors Partial support for > 4 motors, needs a clean way of defining wiring. --- src/boards.h | 14 +++++--- src/config.h | 5 --- src/main.h | 2 ++ src/modules/endstops/endstops.cpp | 4 +-- src/modules/stage/stage.cpp | 55 ++++++++++++++++++++++--------- src/modules/stage/stage.h | 5 ++- 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/boards.h b/src/boards.h index a5f2807..5f02603 100644 --- a/src/boards.h +++ b/src/boards.h @@ -32,6 +32,7 @@ #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #define SUPPORT_EEPROM + #define STAGE_MAX_MOTORS 3 #elif defined(BOARD_SANGABOARDv2) #define BOARD_STRING "Sangaboard v0.2" #define WIRING_MOTOR_X 13, 12, 11, 10 @@ -45,6 +46,7 @@ #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #define SUPPORT_EEPROM + #define STAGE_MAX_MOTORS 3 #elif defined(BOARD_CUSTOM_BLUEPILL) #define BOARD_STRING "Custom Bluepill board" #define WIRING_MOTOR_X PB11, PB10, PB1, PB0 @@ -57,6 +59,7 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define STAGE_MAX_MOTORS 3 #elif defined(BOARD_CUSTOM_PICO) #define BOARD_STRING "Custom Pico board" #define WIRING_MOTOR_X 2, 3, 4, 5 @@ -67,13 +70,14 @@ #define WIRING_ENDSTOPS_MIN {A0,A1,A2} #define WIRING_ENDSTOPS_MAX {A3,A4,A5} #define ADDITIONAL_STEPPERS 1 - #define WIRING_ADDITIONAL_STEPPERS = {{0, 1, 22, 23}} + #define WIRING_ADDITIONAL_STEPPER 0, 1, 22, 23 #define WIRING_STEPSTICK {13,14,15} //enable, step, dir todo: move to boards.h #define PRIMARY_SERIAL_PORT Serial #define SECONDARY_SERIAL_PORT Serial2 #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #define SUPPORT_EEPROM + #define STAGE_MAX_MOTORS 4 #elif defined(BOARD_SANGABOARDv5) #define BOARD_STRING "Sangaboard v0.5" #define WIRING_MOTOR_X 2, 3, 4, 5 @@ -84,7 +88,7 @@ #define WIRING_ENDSTOPS_MIN {26,27,28} //#define WIRING_ENDSTOPS_MAX {22,23,0} //not normally used #define ADDITIONAL_STEPPERS 1 - #define WIRING_ADDITIONAL_STEPPERS = {{0, 1, 22, 23}} + #define WIRING_ADDITIONAL_STEPPER 0, 1, 22, 23 #define WIRING_STEPSTICK {23,22,1} //enable, step, dir todo: move to boards.h #define WIRING_SERIAL_TX 8 #define WIRING_SERIAL_RX 9 @@ -95,6 +99,7 @@ #define WIRING_CC_LED 24 //TODO: 4th motor support #define SUPPORT_EEPROM + #define STAGE_MAX_MOTORS 4 #elif defined(BOARD_CUSTOM_ESP32) #define BOARD_STRING "Custom ESP32 board" #define WIRING_MOTOR_X 34, 32, 25, 27 @@ -108,8 +113,9 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define STAGE_MAX_MOTORS 3 #elif defined(BOARD_CUSTOM) - #define BOARD_STRING "Custom board" + #define BOARD_STRING "Custom UNO board" #define WIRING_MOTOR_X 2, 3, 4, 5 #define WIRING_MOTOR_Y 2, 3, 4, 5 #define WIRING_MOTOR_Z 2, 3, 4, 5 @@ -118,10 +124,10 @@ #define WIRING_ENDSTOPS_MIN {1,2,0} //#define WIRING_ENDSTOPS_MAX {22,23,0} //not normally used #define ADDITIONAL_STEPPERS 1 - #define WIRING_ADDITIONAL_STEPPERS = {{0, 1, 22, 23}} #define WIRING_STEPSTICK {8,9,10} //enable, step, dir todo: move to boards.h #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #define SUPPORT_EEPROM + #define STAGE_MAX_MOTORS 3 #endif \ No newline at end of file diff --git a/src/config.h b/src/config.h index 36c15a6..b56a1d6 100644 --- a/src/config.h +++ b/src/config.h @@ -21,11 +21,6 @@ // #define STEPSTICK #define ILLUMINATION - //module configs - #ifdef STAGE - #define STAGE_N_MOTORS 3 - #endif - #ifdef ENDSTOPS #define ENDSTOPS_MIN //#define ENDSTOPS_MAX diff --git a/src/main.h b/src/main.h index 140c14a..f492713 100644 --- a/src/main.h +++ b/src/main.h @@ -45,5 +45,7 @@ extern void register_module(const Command commands[], void (*loop_fn)(void)); void get_version(String); void get_modules(String); void get_eeprom_last_commit(String); +void get_system_status(String); + #define MAIN_H #endif \ No newline at end of file diff --git a/src/modules/endstops/endstops.cpp b/src/modules/endstops/endstops.cpp index ada01ff..3083fe7 100644 --- a/src/modules/endstops/endstops.cpp +++ b/src/modules/endstops/endstops.cpp @@ -11,8 +11,8 @@ unsigned long retreat_steps = 5000; unsigned long home_move_steps = 100000; -unsigned long axis_max[STAGE_N_MOTORS]; -const uint8_t axis_max_eeprom = sizeof(long)*(STAGE_N_MOTORS+2); +unsigned long axis_max[STAGE_MAX_MOTORS]; +const uint8_t axis_max_eeprom = 200+sizeof(long)*(STAGE_MAX_MOTORS+2); #ifdef ENDSTOPS_MIN const uint8_t endstops_min_pins[] = WIRING_ENDSTOPS_MIN; #endif diff --git a/src/modules/stage/stage.cpp b/src/modules/stage/stage.cpp index ead2f53..a8f0b90 100644 --- a/src/modules/stage/stage.cpp +++ b/src/modules/stage/stage.cpp @@ -10,18 +10,22 @@ #include "main.h" #include +#if MAX_STEPPERS > 3 && !defined(WIRING_ADDITIONAL_STEPPER) +#error "Define WIRING_ADDITIONAL_STEPPER to support 4th stepper" +#endif + // The array below has 3 stepper objects, for X,Y,Z respectively -const uint8_t n_motors = STAGE_N_MOTORS; +uint8_t n_motors = 3; const uint8_t stage_position_eeprom = 0; long min_step_delay = -1; -const uint8_t min_step_delay_eeprom = stage_position_eeprom + sizeof(long) * n_motors; +const uint8_t min_step_delay_eeprom = stage_position_eeprom + sizeof(long) * STAGE_MAX_MOTORS; long ramp_time = -1; const uint8_t ramp_time_eeprom = min_step_delay_eeprom + sizeof(long); const uint8_t axis_max_eeprom = ramp_time_eeprom + sizeof(long); const uint8_t non_blocking_moves_eeprom = axis_max_eeprom + sizeof(long); -Stepper *motors[n_motors]; -signed long current_pos[n_motors]; -long steps_remaining[n_motors]; +Stepper *motors[STAGE_MAX_MOTORS]; +signed long current_pos[STAGE_MAX_MOTORS]; +long steps_remaining[STAGE_MAX_MOTORS]; bool stage_moving = false; bool notify_on_stop = false; @@ -33,7 +37,15 @@ void stage_setup() motors[0] = new Stepper(8, WIRING_MOTOR_X); motors[1] = new Stepper(8, WIRING_MOTOR_Y); motors[2] = new Stepper(8, WIRING_MOTOR_Z); - EACH_MOTOR + #if STAGE_MAX_MOTORS > 3 + //TODO: support >4 stepppers + for (int i = 0; i < STAGE_MAX_MOTORS - 3; i++) + { + motors[i+3] = new Stepper(8, WIRING_ADDITIONAL_STEPPER); + } + #endif + + EACH_MAX_MOTOR { motors[i]->setSpeed(10); // as a default set to 10 rpm, though this is ignored... steps_remaining[i] = 0; @@ -85,13 +97,13 @@ void print_position() } unsigned long move_start_time = 0; -unsigned long distance_moved[n_motors]; -long displacement[n_motors]; +unsigned long distance_moved[STAGE_MAX_MOTORS]; +long displacement[STAGE_MAX_MOTORS]; float final_scaled_t; -float step_delay[n_motors]; -int8_t move_directions[n_motors]; +float step_delay[STAGE_MAX_MOTORS]; +int8_t move_directions[STAGE_MAX_MOTORS]; -void start_move(long displ[n_motors]) +void start_move(long displ[STAGE_MAX_MOTORS]) { // move all the axes in a nice move // split displacements into magnitude and direction, and find max. travel @@ -219,13 +231,10 @@ void stage_mrz(String command) void stage_mr(String command) { - char *args[3]; - parse_arguments(args, command, 3); + char *args[STAGE_MAX_MOTORS]; + parse_arguments(args, command, n_motors); EACH_MOTOR { - if (i>3) - continue; - displacement[i] = atol(args[i]); free(args[i]); } @@ -300,6 +309,19 @@ void update_blocking_moves(String command) } } +void stage_n_motors(String command) +{ + char *args[1]; + parse_arguments(args, command, 1); + if (args[0][0] != '?') + n_motors = atoi(args[0]); + + comPort->print("n_motors "); + comPort->println(n_motors); + + free(args[0]); +} + void stage_zero(String command) { EACH_MOTOR current_pos[i] = 0; @@ -348,4 +370,5 @@ extern const Command stage_commands[] = { {"moving", is_stage_moving}, {"notify_on_stop", activate_notify_on_stop}, {"blocking_moves", update_blocking_moves}, + {"n_motors", stage_n_motors}, END_COMMAND}; \ No newline at end of file diff --git a/src/modules/stage/stage.h b/src/modules/stage/stage.h index 68b0b16..942a528 100644 --- a/src/modules/stage/stage.h +++ b/src/modules/stage/stage.h @@ -3,7 +3,9 @@ #include "main.h" #include -#define EACH_MOTOR for (int i = 0; i < STAGE_N_MOTORS; i++) +#define EACH_MOTOR for (int i = 0; i < n_motors; i++) +#define EACH_MAX_MOTOR for (int i = 0; i < STAGE_MAX_MOTORS; i++) + void stage_setup(); void stage_loop(); @@ -21,6 +23,7 @@ void stage_ramp_time(String command); void stage_zero(String command); void start_move(long displ[]); void stage_stop(String command); +void stage_n_motors(String command); extern const Command stage_commands[]; //we want to expose these for endstops (and potentially other modules)