Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed tinyshell X_NUCLEO_IHM02A1
Diff: main.cpp
- Revision:
- 29:a510e875936d
- Parent:
- 28:19b25daa7777
- Child:
- 30:a23204ee2be2
--- a/main.cpp Thu Apr 01 00:43:00 2021 +0000 +++ b/main.cpp Thu Apr 01 18:03:26 2021 +0000 @@ -161,18 +161,30 @@ /* Nasty globals haha */ bool limit_flag = false; bool status_flag = false; -char cmd_buffer[CMD_BUFFER_SIZE]; -char output_buffer[OUTPUT_BUFFER_SIZE]; +bool run_flag = false; +bool stop_flag = false; /* Tinyshell command handler functions */ -void print_status(int argc, char **argv) +void set_status_flag(int argc, char **argv) { status_flag = true; } +void set_run_flag(int argc, char **argv) +{ + run_flag = true; +} + +void set_stop_flag(int argc, char **argv) +{ + stop_flag = true; +} + // parent cmd (0 for top) cmd input name, help string, usage string, // function to launch, arg when called, next (0 at init), child (0 at init) -tinysh_cmd_t print_status_cmd = {0, "status", "status command", "[args]", print_status,0,0,0}; +tinysh_cmd_t print_status_cmd = {0, "status", "status command", "[args]", set_status_flag,0,0,0}; +tinysh_cmd_t run_cmd = {0, "run", "run command", "[args]", set_run_flag,0,0,0}; +tinysh_cmd_t stop_cmd = {0, "stop", "stop command", "[args]", set_stop_flag,0,0,0}; /* mandatory tiny shell output function */ void tinysh_char_out(unsigned char c) @@ -194,6 +206,8 @@ // Add all tinyshell commands here tinysh_add_command(&print_status_cmd); + tinysh_add_command(&run_cmd); + tinysh_add_command(&stop_cmd); /* Initializing SPI bus. */ #ifdef TARGET_STM32F429 @@ -207,19 +221,14 @@ /* Building a list of motor control components. */ L6470 **motors = x_nucleo_ihm02a1->get_components(); - - // Hello world: just run the damn motor forever - printf("Running.\r\n"); - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_run(StepperMotor::BWD, SPEED_SPS); - } - x_nucleo_ihm02a1->perform_prepared_actions(); - - + // Main loop while(1) { /* 1: Check for hardware flags ----------------------------------------*/ if (limit_flag) { + // clear flag + limit_flag = false; + // Hard stop for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { motors[m]->prepare_hard_stop(); @@ -235,14 +244,44 @@ x_nucleo_ihm02a1->perform_prepared_actions(); printf("Reached limit.\r\n"); - limit_flag = false; } /* 2: Fetch new chars for Tinyshell ----------------------------------*/ tinysh_char_in(pc.getc()); /* 3: Handle Commands ------------------------------------------------*/ + if (run_flag) { + run_flag = false; + // Hello world: just run the damn motor forever + printf("Running.\r\n"); + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_run(StepperMotor::BWD, SPEED_SPS); + } + x_nucleo_ihm02a1->perform_prepared_actions(); + } + + if (stop_flag) { + stop_flag = false; + // Hard stop + printf("Stopping.\r\n"); + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_hard_stop(); + } + x_nucleo_ihm02a1->perform_prepared_actions(); + + wait_ms(HARD_STOP_WAIT_MS); + + // High Z + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_hard_hiz(); + } + x_nucleo_ihm02a1->perform_prepared_actions(); + } + if (status_flag) { + // clear flag + status_flag = false; + // Fetch and parse the status register for each motor for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { status_bytes[m] = motors[m]->get_status();