From ce762bdb60839495e4e787eea18df022f7f28a71 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Mon, 13 Feb 2023 15:18:16 +0000 Subject: [PATCH 01/19] Added an environment for pico using HW serial I wasn't sure how to compile with or without HW serial support, so I duplicated the "pico" environment and added a -D HW_SERIAL --- platformio.ini | 8 ++++++++ src/config.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index d468130..3b79324 100644 --- a/platformio.ini +++ b/platformio.ini @@ -64,6 +64,14 @@ build_flags = -D MCU_ESP32 monitor_filters = esp32_exception_decoder build_type = debug +[env:picohw] +platform = https://github.com/maxgerhardt/platform-raspberrypi.git +board = pico +framework = arduino +board_build.core = earlephilhower +upload_port = /media/filip/RPI-RP2 +build_flags = -D MCU_PICO -D HW_SERIAL + [env:pico] platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = pico diff --git a/src/config.h b/src/config.h index b458b73..92b1f6e 100644 --- a/src/config.h +++ b/src/config.h @@ -3,9 +3,11 @@ #define BOARD_AUTO //#define BOARD_SANGABOARDv5 - //without this, default to HW serial where available + //if HW_SERIAL is not defined, we'll use USB serial communication //the details are board specific and configured in boards.h + #ifndef HW_SERIAL #define USB_SERIAL + #endif #include //defines wiring From e65c290ae6b14f088e30c64654d63f000404fb8a Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Mon, 13 Feb 2023 15:18:33 +0000 Subject: [PATCH 02/19] Ignore VSCode suggestion --- .vscode/extensions.json | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} From 3997d66cdee88ff60a7a7663fb5caa36471bf27b Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 12:05:48 +0000 Subject: [PATCH 03/19] Merge in com port switching from sangaboard v4 I've refactored the use of the serial port, to store the current serial port in a global variable in a module. This is set to the most recent port to receive data, and so we should always be replying to the port that was used to initiate a command. If SECONDARY_SERIAL_PORT is not defined, this should result in no change - if it is, we should be able to communicate on either port. --- lib/ComPort/ComPort.cpp | 4 + lib/ComPort/ComPort.h | 9 +++ src/boards.h | 17 +++-- src/main.cpp | 46 +++++++---- src/main.h | 3 +- src/modules/endstops/endstops.cpp | 35 ++++----- src/modules/help/help.cpp | 93 ++++++++++++----------- src/modules/illumination/illumination.cpp | 19 ++--- src/modules/light_sensor/light_sensor.cpp | 47 ++++++------ src/modules/stage/stage.cpp | 39 +++++----- src/modules/stepstick/stepstick.cpp | 9 ++- test/test_argument_parsing.cpp | 4 +- 12 files changed, 180 insertions(+), 145 deletions(-) create mode 100644 lib/ComPort/ComPort.cpp create mode 100644 lib/ComPort/ComPort.h diff --git a/lib/ComPort/ComPort.cpp b/lib/ComPort/ComPort.cpp new file mode 100644 index 0000000..94443bb --- /dev/null +++ b/lib/ComPort/ComPort.cpp @@ -0,0 +1,4 @@ +#include + +// Actually define the variable and default to the USB Serial port +HardwareSerial* comPort = (HardwareSerial*) &Serial; \ No newline at end of file diff --git a/lib/ComPort/ComPort.h b/lib/ComPort/ComPort.h new file mode 100644 index 0000000..a5af2ac --- /dev/null +++ b/lib/ComPort/ComPort.h @@ -0,0 +1,9 @@ +#ifndef COMPORT_H +#define COMPORT_H + +#include + +// comPort is a Serial-like object that we can switch. +extern HardwareSerial* comPort; + +#endif \ No newline at end of file diff --git a/src/boards.h b/src/boards.h index ef93337..42699c1 100644 --- a/src/boards.h +++ b/src/boards.h @@ -28,7 +28,7 @@ #define ENDSTOPS_PULLUPS true #define WIRING_ENDSTOPS_MIN {A0,A1,A2} #define WIRING_ENDSTOPS_MAX {A3,A4,A5} - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #elif defined(BOARD_SANGABOARDv2) @@ -40,7 +40,7 @@ #define ENDSTOPS_PULLUPS true #define WIRING_ENDSTOPS_MIN {A0,A1,A2} #define WIRING_ENDSTOPS_MAX {A3,A4,A5} - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #elif defined(BOARD_CUSTOM_BLUEPILL) @@ -52,7 +52,7 @@ #define ENDSTOPS_PULLUPS true #define WIRING_ENDSTOPS_MIN {A0,A1,A2} #define WIRING_ENDSTOPS_MAX {A3,A4,A5} - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #elif defined(BOARD_CUSTOM_PICO) @@ -67,7 +67,7 @@ #define ADDITIONAL_STEPPERS 1 #define WIRING_ADDITIONAL_STEPPERS = {{0, 1, 22, 23}} #define WIRING_STEPSTICK {13,14,15} //enable, step, dir todo: move to boards.h - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #elif defined(BOARD_SANGABOARDv5) @@ -85,9 +85,10 @@ #define WIRING_SERIAL_TX 8 #define WIRING_SERIAL_RX 9 #ifdef USB_SERIAL - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #else - #define SERIAL_PORT Serial2 + #define PRIMARY_SERIAL_PORT Serial + #define SECONDARY_SERIAL_PORT Serial2 #endif #define PWM_NUM 2 #define WIRING_PWM_LEDS {25, 29} @@ -103,7 +104,7 @@ #define WIRING_ENDSTOPS_MIN {A0,A1,A2} #define WIRING_ENDSTOPS_MAX {A3,A4,A5} #define WIRING_STEPSTICK {13,12,27} - #define SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #elif defined(BOARD_CUSTOM) @@ -118,7 +119,7 @@ #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 SERIAL_PORT Serial + #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c8a19ab..fc56122 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "config.h" #include #include +#include #ifdef HELP #include "modules/help/help.h" @@ -73,9 +74,9 @@ void handle_command(String received_command) } #ifdef HELP - SERIAL_PORT.println(F("Type 'help' for a list of commands.")); + comPort->println(F("Type 'help' for a list of commands.")); #else - SERIAL_PORT.println(F("Invalid command")); + comPort->println(F("Invalid command")); #endif } @@ -104,32 +105,32 @@ uint8_t parse_arguments(char **arguments, String command, uint8_t max_args) void get_version(String) { - SERIAL_PORT.println(F(VERSION_STRING)); + comPort->println(F(VERSION_STRING)); return; } void get_modules(String) { #if defined(LIGHTSENSOR) && defined(LIGHT_SENSOR_ADAFRUIT_TSL2591) - SERIAL_PORT.println(F("Light Sensor: TSL2591")); + comPort->println(F("Light Sensor: TSL2591")); #elif defined(LIGHTSENSOR) && defined(LIGHT_SENSOR_ADAFRUIT_ADS1115) - SERIAL_PORT.println(F("Light Sensor: ADS1115")); + comPort->println(F("Light Sensor: ADS1115")); #endif #if defined(ENDSTOPS) - SERIAL_PORT.print("Endstops:"); + comPort->print("Endstops:"); #ifdef ENDSTOPS_MIN - SERIAL_PORT.print(" min"); + comPort->print(" min"); #endif #ifdef ENDSTOPS_MAX - SERIAL_PORT.print(" max"); + comPort->print(" max"); #endif #ifdef ENDSTOPS_SOFT - SERIAL_PORT.print(" soft"); + comPort->print(" soft"); #endif - SERIAL_PORT.println(); + comPort->println(); #endif - SERIAL_PORT.println("--END--"); + comPort->println("--END--"); } #ifndef UNIT_TEST @@ -141,9 +142,15 @@ void setup() Serial2.setRX(WIRING_SERIAL_RX); #endif - SERIAL_PORT.begin(115200); - while (!SERIAL_PORT) + comPort = (HardwareSerial*) &PRIMARY_SERIAL_PORT; + PRIMARY_SERIAL_PORT->begin(115200); + while (!PRIMARY_SERIAL_PORT) delay(1); + #if defined(SECONDARY_SERIAL_PORT) + SECONDARY_SERIAL_PORT->begin(115200); + while (!SECONDARY_SERIAL_PORT) + delay(1); + #endif register_module(core_commands, NULL); @@ -173,14 +180,21 @@ void setup() #endif registered_commands[registered_commands_count] = &end_command; - SERIAL_PORT.println(F(VERSION_STRING)); + comPort->println(F(VERSION_STRING)); } void loop() { - if (SERIAL_PORT.available()) + if (PRIMARY_SERIAL_PORT.available()) { - handle_command(SERIAL_PORT.readStringUntil('\n')); + comPort = (HardwareSerial*) &PRIMARY_SERIAL_PORT; + handle_command(comPort->readStringUntil('\n')); + } + #if defined(SECONDARY_SERIAL_PORT) + if (SECONDARY_SERIAL_PORT.available()) + { + comPort = (HardwareSerial*) &SECONDARY_SERIAL_PORT; + handle_command(comPort->readStringUntil('\n')); } for (int i = 0; i < registered_loop_fn_count; i++) diff --git a/src/main.h b/src/main.h index e69ec23..2ca26a8 100644 --- a/src/main.h +++ b/src/main.h @@ -1,8 +1,9 @@ #ifndef MAIN_H #include "config.h" #include +#include #ifdef DEBUG_ON -#define D(x) Serial.println(x) +#define D(x) comPort->println(x) #else #define D(x) #endif diff --git a/src/modules/endstops/endstops.cpp b/src/modules/endstops/endstops.cpp index 330c1c9..ada01ff 100644 --- a/src/modules/endstops/endstops.cpp +++ b/src/modules/endstops/endstops.cpp @@ -2,6 +2,7 @@ #ifdef ENDSTOPS #include "endstops.h" #include "../stage/stage.h" +#include #ifdef SUPPORT_EEPROM #include #else @@ -99,7 +100,7 @@ void endstops_loop() homing_final = false; min_step_delay = previous_step_delay; home_retreat(); - SERIAL_PORT.println("done."); + comPort->println("done."); return; } } @@ -121,8 +122,8 @@ int8_t endstops_check() if (endstop_break != 0 && stage_moving) { - SERIAL_PORT.print(F("Endstop hit:")); - SERIAL_PORT.println(endstop_break); + comPort->print(F("Endstop hit:")); + comPort->println(endstop_break); //if we have both min/max endstops, axis_max is adjusted to the correct value //if we only have min, we go from 0 -> predefined axis_max //if we only have max, we go from 0 -> predefined axis_max @@ -154,16 +155,16 @@ void endstops_status(String command) EACH_MOTOR { if (i > 0) - SERIAL_PORT.print(" "); + comPort->print(" "); if (endstop_triggered(i, -1)) - SERIAL_PORT.print("-1"); + comPort->print("-1"); else if (endstop_triggered(i, 1)) - SERIAL_PORT.print("1"); + comPort->print("1"); else - SERIAL_PORT.print("0"); + comPort->print("0"); } - SERIAL_PORT.println(); + comPort->println(); } void print_axes_max(String command) @@ -171,10 +172,10 @@ void print_axes_max(String command) EACH_MOTOR { if (i > 0) - SERIAL_PORT.print(" "); - SERIAL_PORT.print(axis_max[i]); + comPort->print(" "); + comPort->print(axis_max[i]); } - SERIAL_PORT.println(); + comPort->println(); } void home_start(uint8_t axes, int8_t direction) @@ -226,7 +227,7 @@ void home_final() void endstops_home_min(String command) { #ifndef ENDSTOPS_MIN - SERIAL_PORT.println(F("Min endstops not installed")); + comPort->println(F("Min endstops not installed")); return; #endif if (command.equals("home_min")) @@ -241,7 +242,7 @@ void endstops_home_min(String command) void endstops_home_max(String command) { #ifndef ENDSTOPS_MAX - SERIAL_PORT.println(F("Max endstops not installed")); + comPort->println(F("Max endstops not installed")); return; #endif if (command.equals("home_max")) @@ -293,10 +294,10 @@ void endstops_setup() } //endstops? - get triggered endstops in (1,0,-1) format for max, none, min")); -// SERIAL_PORT.println(F("home_min - home given (00000zyx byte, e.g. 1 for x) or all axes to their min position")); -// SERIAL_PORT.println(F("home_max - home given (00000zyx byte, e.g. 3 for x and y) or all axes to their max position")); -// SERIAL_PORT.println(F("max_p? - return positions of max endstops")); -// SERIAL_PORT.println(F("max +// comPort->println(F("home_min - home given (00000zyx byte, e.g. 1 for x) or all axes to their min position")); +// comPort->println(F("home_max - home given (00000zyx byte, e.g. 3 for x and y) or all axes to their max position")); +// comPort->println(F("max_p? - return positions of max endstops")); +// comPort->println(F("max extern const Command endstops_commands[] = { {"endstops?", endstops_status}, {"home_min", endstops_home_min}, diff --git a/src/modules/help/help.cpp b/src/modules/help/help.cpp index c530481..dbc8027 100644 --- a/src/modules/help/help.cpp +++ b/src/modules/help/help.cpp @@ -1,6 +1,7 @@ #include "config.h" #ifdef HELP #include "help.h" +#include void help_setup() { @@ -9,78 +10,78 @@ void help_setup() void help(String command) { - SERIAL_PORT.println(""); - SERIAL_PORT.print("Board: "); - SERIAL_PORT.println(F(BOARD_STRING)); + comPort->println(""); + comPort->print("Board: "); + comPort->println(F(BOARD_STRING)); #ifdef LIGHT_SENSOR #if defined ADAFRUIT_TSL2591 - SERIAL_PORT.println(F("Compiled with Adafruit TSL2591 support")); + comPort->println(F("Compiled with Adafruit TSL2591 support")); #elif defined ADAFRUIT_ADS1115 - SERIAL_PORT.println(F("Compiled with Adafruit ADS1115 support")); + comPort->println(F("Compiled with Adafruit ADS1115 support")); #endif #endif //LIGHT_SENSOR #ifdef ENDSTOPS #ifdef ENDSTOPS_MIN - SERIAL_PORT.println(F("Compiled with min endstops support")); + comPort->println(F("Compiled with min endstops support")); #endif #ifdef ENDSTOPS_MAX - SERIAL_PORT.println(F("Compiled with max endstops support")); + comPort->println(F("Compiled with max endstops support")); #endif #endif //ENDSTOPS - SERIAL_PORT.println(""); - SERIAL_PORT.println(F("Commands (terminated by a newline character):")); + comPort->println(""); + comPort->println(F("Commands (terminated by a newline character):")); #ifdef STAGE - SERIAL_PORT.println(F("mrx - relative move in x")); - SERIAL_PORT.println(F("mry - relative move in y")); - SERIAL_PORT.println(F("mrz - relative move in z")); - SERIAL_PORT.println(F("mr - relative move in all 3 axes")); - SERIAL_PORT.println(F("release - de-energise all motors")); - SERIAL_PORT.println(F("p? - print position (3 space-separated integers")); - SERIAL_PORT.println(F("ramp_time - set the time taken to accelerate/decelerate in us")); - SERIAL_PORT.println(F("min_step_delay - set the minimum time between steps in us.")); - SERIAL_PORT.println(F("dt - set the minimum time between steps in us.")); - SERIAL_PORT.println(F("ramp_time? - get the time taken to accelerate/decelerate in us")); - SERIAL_PORT.println(F("min_step_delay? - get the minimum time between steps in us.")); - SERIAL_PORT.println(F("zero - set the current position to zero.")); - SERIAL_PORT.println(F("stop - stop a move in progress.")); - SERIAL_PORT.println(F("blocking_moves? - get blocking moves enabled")); - SERIAL_PORT.println(F("blocking_moves - enable/disable blocking moves")); - SERIAL_PORT.println(F("moving? - check if a move is in progress")); - SERIAL_PORT.println(F("notify_on_stop - respond with stopped when current move finishes")); + comPort->println(F("mrx - relative move in x")); + comPort->println(F("mry - relative move in y")); + comPort->println(F("mrz - relative move in z")); + comPort->println(F("mr - relative move in all 3 axes")); + comPort->println(F("release - de-energise all motors")); + comPort->println(F("p? - print position (3 space-separated integers")); + comPort->println(F("ramp_time - set the time taken to accelerate/decelerate in us")); + comPort->println(F("min_step_delay - set the minimum time between steps in us.")); + comPort->println(F("dt - set the minimum time between steps in us.")); + comPort->println(F("ramp_time? - get the time taken to accelerate/decelerate in us")); + comPort->println(F("min_step_delay? - get the minimum time between steps in us.")); + comPort->println(F("zero - set the current position to zero.")); + comPort->println(F("stop - stop a move in progress.")); + comPort->println(F("blocking_moves? - get blocking moves enabled")); + comPort->println(F("blocking_moves - enable/disable blocking moves")); + comPort->println(F("moving? - check if a move is in progress")); + comPort->println(F("notify_on_stop - respond with stopped when current move finishes")); #endif //STAGE #ifdef LIGHT_SENSOR - SERIAL_PORT.println(F("light_sensor_gain - set the gain of the light sensor")); - SERIAL_PORT.println(F("light_sensor_gain? - get the gain of the light sensor")); - SERIAL_PORT.println(F("light_sensor_gain_values? - get the allowable gain values of the light sensor")); - SERIAL_PORT.println(F("light_sensor_integration_time? - get the integration time in milliseconds")); - SERIAL_PORT.println(F("light_sensor_intensity? - read the current value from the full spectrum diode")); + comPort->println(F("light_sensor_gain - set the gain of the light sensor")); + comPort->println(F("light_sensor_gain? - get the gain of the light sensor")); + comPort->println(F("light_sensor_gain_values? - get the allowable gain values of the light sensor")); + comPort->println(F("light_sensor_integration_time? - get the integration time in milliseconds")); + comPort->println(F("light_sensor_intensity? - read the current value from the full spectrum diode")); #endif //LIGHT_SENSOR #if defined(ENDSTOPS_MIN) || defined(ENDSTOPS_MAX) - SERIAL_PORT.println(F("endstops? - get triggered endstops in (1,0,-1) format for max, none, min")); - SERIAL_PORT.println(F("home_min - home given (00000zyx byte, e.g. 1 for x) or all axes to their min position")); - SERIAL_PORT.println(F("home_max - home given (00000zyx byte, e.g. 3 for x and y) or all axes to their max position")); - SERIAL_PORT.println(F("max_p? - return positions of max endstops")); - SERIAL_PORT.println(F("max - set maximum positions")); + comPort->println(F("endstops? - get triggered endstops in (1,0,-1) format for max, none, min")); + comPort->println(F("home_min - home given (00000zyx byte, e.g. 1 for x) or all axes to their min position")); + comPort->println(F("home_max - home given (00000zyx byte, e.g. 3 for x and y) or all axes to their max position")); + comPort->println(F("max_p? - return positions of max endstops")); + comPort->println(F("max - set maximum positions")); #endif #ifdef STEPSTICK - SERIAL_PORT.println(F("stepstick_move - move stepper by with max speed ")); - SERIAL_PORT.println(F("stepstick_release - release stepstick stepper")); - SERIAL_PORT.println(F("stepstick_stop - abort stepstick move")); + comPort->println(F("stepstick_move - move stepper by with max speed ")); + comPort->println(F("stepstick_release - release stepstick stepper")); + comPort->println(F("stepstick_stop - abort stepstick move")); #endif - //SERIAL_PORT.println(F("test_mode - set test_mode ")); - SERIAL_PORT.println(F("version - get firmware version string")); - SERIAL_PORT.println(""); - SERIAL_PORT.println("Input Key:"); - SERIAL_PORT.println(F(" - a decimal integer.")); - SERIAL_PORT.println(""); - SERIAL_PORT.println("--END--"); + //comPort->println(F("test_mode - set test_mode ")); + comPort->println(F("version - get firmware version string")); + comPort->println(""); + comPort->println("Input Key:"); + comPort->println(F(" - a decimal integer.")); + comPort->println(""); + comPort->println("--END--"); } #endif //HELP \ No newline at end of file diff --git a/src/modules/illumination/illumination.cpp b/src/modules/illumination/illumination.cpp index c82a825..e5b6c26 100644 --- a/src/modules/illumination/illumination.cpp +++ b/src/modules/illumination/illumination.cpp @@ -1,6 +1,7 @@ #include "config.h" #ifdef ILLUMINATION #include "illumination.h" +#include #ifdef SUPPORT_EEPROM #include #else @@ -16,11 +17,11 @@ void illumination_pwm_freq(String command) analogWriteFreq(atol(args[0]));//32khz seems sensible analogWriteRange(65535); - SERIAL_PORT.print("Frequency set: "); - SERIAL_PORT.print(atol(args[0])); - SERIAL_PORT.println("Hz"); + comPort->print("Frequency set: "); + comPort->print(atol(args[0])); + comPort->println("Hz"); #else - SERIAL_PORT.println("Feature not supported"); + comPort->println("Feature not supported"); #endif free(args[0]); @@ -35,7 +36,7 @@ void illumination_pwm(String command) uint8_t index = atoi(args[0]); if (index >= PWM_NUM) { - Serial.println("Invalid index"); + comPort->println("Invalid index"); } float val = atof(args[1]); @@ -50,8 +51,8 @@ void illumination_pwm(String command) #endif analogWrite(pwm_led_pins[index], pwm_val); - SERIAL_PORT.print("Illumination set"); - SERIAL_PORT.println(pwm_val); + comPort->print("Illumination set"); + comPort->println(pwm_val); free(args[0]); free(args[1]); @@ -117,10 +118,10 @@ void cc_set(String command) delayMicroseconds(stepd); digitalWrite(WIRING_CC_LED, HIGH); delayMicroseconds(td); - Serial.println("Stepping CC"); //TODO remove debug print + comPort->println("Stepping CC"); //TODO remove debug print } #else - SERIAL_PORT.println("CC LED not supported"); + comPort->println("CC LED not supported"); #endif } diff --git a/src/modules/light_sensor/light_sensor.cpp b/src/modules/light_sensor/light_sensor.cpp index 70b89e6..b4f33c1 100644 --- a/src/modules/light_sensor/light_sensor.cpp +++ b/src/modules/light_sensor/light_sensor.cpp @@ -1,4 +1,5 @@ #include "config.h" +#include #ifdef LIGHT_SENSOR #include "light_sensor.h" #include "config.h" @@ -54,35 +55,35 @@ void setup_light_sensor_device() } else { - SERIAL_PORT.println(F("No light sensor found. NB your board will start up faster if you recompile without light sensor support.")); + comPort->println(F("No light sensor found. NB your board will start up faster if you recompile without light sensor support.")); } } void print_light_sensor_gain() { // Print the current gain value of the light sensor - SERIAL_PORT.print(F("light sensor gain ")); + comPort->print(F("light sensor gain ")); tsl2591Gain_t gain = tsl.getGain(); switch (gain) { case TSL2591_GAIN_LOW: - SERIAL_PORT.println(F("1x (Low)")); + comPort->println(F("1x (Low)")); break; case TSL2591_GAIN_MED: - SERIAL_PORT.println(F("25x (Medium)")); + comPort->println(F("25x (Medium)")); break; case TSL2591_GAIN_HIGH: - SERIAL_PORT.println(F("428x (High)")); + comPort->println(F("428x (High)")); break; case TSL2591_GAIN_MAX: - SERIAL_PORT.println(F("9876x (Max)")); + comPort->println(F("9876x (Max)")); break; } } void light_sensor_gain_values(String command) { // Print the allowable gain values of the light sensor - SERIAL_PORT.println(F("light sensor gains: 1x, 25x, 428x, 9876x")); + comPort->println(F("light sensor gains: 1x, 25x, 428x, 9876x")); } void set_light_sensor_gain(int newgain) @@ -103,7 +104,7 @@ void set_light_sensor_gain(int newgain) tsl.setGain(TSL2591_GAIN_MAX); break; default: - SERIAL_PORT.println(F("Error: gain may only be 1, 25, 428, or 9876.")); + comPort->println(F("Error: gain may only be 1, 25, 428, or 9876.")); return; } print_light_sensor_gain(); @@ -112,16 +113,16 @@ void set_light_sensor_gain(int newgain) void light_sensor_integration_time(String command) { // Print the current integration time in milliseconds. - SERIAL_PORT.print(F("light sensor integration time ")); - SERIAL_PORT.print((tsl.getTiming() + 1) * 100, DEC); - SERIAL_PORT.println(F(" ms")); + comPort->print(F("light sensor integration time ")); + comPort->print((tsl.getTiming() + 1) * 100, DEC); + comPort->println(F(" ms")); } void light_sensor_intensity(String command) { // Print the current light value uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM); - SERIAL_PORT.println(x, DEC); + comPort->println(x, DEC); } #endif // ADAFRUIT_TSL2591 @@ -137,34 +138,34 @@ void setup_light_sensor_device() void print_light_sensor_gain() { // Print the current gain value of the light sensor - SERIAL_PORT.print(F("light sensor gain ")); + comPort->print(F("light sensor gain ")); adsGain_t gain = ads.getGain(); switch (gain) { case GAIN_TWOTHIRDS: - SERIAL_PORT.println(F("0.66x (specify 0)")); + comPort->println(F("0.66x (specify 0)")); break; case GAIN_ONE: - SERIAL_PORT.println(F("1x")); + comPort->println(F("1x")); break; case GAIN_TWO: - SERIAL_PORT.println(F("2x")); + comPort->println(F("2x")); break; case GAIN_FOUR: - SERIAL_PORT.println(F("4x")); + comPort->println(F("4x")); break; case GAIN_EIGHT: - SERIAL_PORT.println(F("8x")); + comPort->println(F("8x")); break; case GAIN_SIXTEEN: - SERIAL_PORT.println(F("16x")); + comPort->println(F("16x")); break; } } void light_sensor_gain_values(String command) { // Print the allowable gain values of the light sensor - SERIAL_PORT.println(F("light sensor gains: 0.66x (specify 0), 1x, 2x, 4x, 8x, 16x")); + comPort->println(F("light sensor gains: 0.66x (specify 0), 1x, 2x, 4x, 8x, 16x")); } void set_light_sensor_gain(int newgain) @@ -191,7 +192,7 @@ void set_light_sensor_gain(int newgain) ads.setGain(GAIN_SIXTEEN); break; default: - SERIAL_PORT.println(F("Error: gain may only be 0, 1, 2, 4, 8, 16 (0 means 2/3).")); + comPort->println(F("Error: gain may only be 0, 1, 2, 4, 8, 16 (0 means 2/3).")); return; } print_light_sensor_gain(); @@ -200,7 +201,7 @@ void set_light_sensor_gain(int newgain) void light_sensor_integration_time(String command) { // Print the current integration time in milliseconds. - SERIAL_PORT.println(F("integration time not supported for ADS1115")); + comPort->println(F("integration time not supported for ADS1115")); } void light_sensor_intensity(String command) @@ -215,7 +216,7 @@ void light_sensor_intensity(String command) uint16_t x = ads.readADC_SingleEnded(ch); //single ended measurement on pin ch (0-3) //uint16_t x = ads.readADC_Differential_0_1(); //differential measurement on pins 0,1 - SERIAL_PORT.println(x, DEC); + comPort->println(x, DEC); } #endif // ADAFRUIT_ADS1115 #endif \ No newline at end of file diff --git a/src/modules/stage/stage.cpp b/src/modules/stage/stage.cpp index b7b8a51..c228c86 100644 --- a/src/modules/stage/stage.cpp +++ b/src/modules/stage/stage.cpp @@ -8,6 +8,7 @@ #include "dummyEEPROM.h" #endif #include "main.h" +#include // The array below has 3 stepper objects, for X,Y,Z respectively const uint8_t n_motors = STAGE_N_MOTORS; @@ -76,10 +77,10 @@ void print_position() EACH_MOTOR { if (i > 0) - SERIAL_PORT.print(" "); - SERIAL_PORT.print(current_pos[i]); + comPort->print(" "); + comPort->print(current_pos[i]); } - SERIAL_PORT.println(); + comPort->println(); } unsigned long move_start_time = 0; @@ -124,7 +125,7 @@ void start_move(long displ[n_motors]) } else { - SERIAL_PORT.println("done."); + comPort->println("done."); } } @@ -183,7 +184,7 @@ void stage_loop() if (!stage_moving && notify_on_stop) { - SERIAL_PORT.println("stopped"); + comPort->println("stopped"); notify_on_stop = false; } } @@ -233,7 +234,7 @@ void stage_release(String command) { releaseMotor(i); } - SERIAL_PORT.println("done"); + comPort->println("done"); } void stage_p(String command) @@ -247,14 +248,14 @@ void stage_min_step_delay(String command) parse_arguments(args, command, 1); if (args[0][0] == '?') { - SERIAL_PORT.print("minimum step delay "); - SERIAL_PORT.println(min_step_delay); + comPort->print("minimum step delay "); + comPort->println(min_step_delay); } else { min_step_delay = atol(args[0]); EEPROM.put(min_step_delay_eeprom, min_step_delay); - SERIAL_PORT.println("done."); + comPort->println("done."); } free(args[0]); } @@ -265,14 +266,14 @@ void stage_ramp_time(String command) parse_arguments(args, command, 1); if (args[0][0] == '?') { - SERIAL_PORT.print("ramp_time "); - SERIAL_PORT.println(ramp_time); + comPort->print("ramp_time "); + comPort->println(ramp_time); } else { ramp_time = atol(args[0]); EEPROM.put(ramp_time_eeprom, ramp_time); - SERIAL_PORT.println("done."); + comPort->println("done."); } free(args[0]); } @@ -283,41 +284,41 @@ void update_blocking_moves(String command) parse_arguments(args, command, 1); if (args[0][0] == '?') { - SERIAL_PORT.print("blocking_moves "); - SERIAL_PORT.println(non_blocking_moves ? "false" : "true"); + comPort->print("blocking_moves "); + comPort->println(non_blocking_moves ? "false" : "true"); } else { non_blocking_moves = !(args[0][0] == 't'); EEPROM.put(non_blocking_moves_eeprom, non_blocking_moves); - SERIAL_PORT.println("done."); + comPort->println("done."); } } void stage_zero(String command) { EACH_MOTOR current_pos[i] = 0; - SERIAL_PORT.println(F("position reset to 0 0 0")); + comPort->println(F("position reset to 0 0 0")); EEPROM.put(0, current_pos); } void stage_stop(String command) { stage_moving = false; - SERIAL_PORT.println("Move aborted"); + comPort->println("Move aborted"); } void is_stage_moving(String command) { //TODO: should I bother checking for a ? - SERIAL_PORT.println(stage_moving ? "true" : "false"); + comPort->println(stage_moving ? "true" : "false"); } void activate_notify_on_stop(String command) { if (!stage_moving) { - SERIAL_PORT.println("Error: stage is not moving"); + comPort->println("Error: stage is not moving"); } else { diff --git a/src/modules/stepstick/stepstick.cpp b/src/modules/stepstick/stepstick.cpp index 6a26c8a..d364fd3 100644 --- a/src/modules/stepstick/stepstick.cpp +++ b/src/modules/stepstick/stepstick.cpp @@ -1,6 +1,7 @@ #include "config.h" #ifdef STEPSTICK #include "stepstick.h" +#include #ifdef SUPPORT_EEPROM #include #else @@ -16,13 +17,13 @@ bool moving = false; void stepstick_release(String command) { digitalWrite(stepstick_pins[0], HIGH);//start with motor disabled - SERIAL_PORT.println("Stepstick released"); + comPort->println("Stepstick released"); } void stepstick_stop(String command) { stepper.stop(); - SERIAL_PORT.println("Stepstick stopping"); + comPort->println("Stepstick stopping"); } void stepstick_move(String command) @@ -37,7 +38,7 @@ void stepstick_move(String command) stepper.moveTo(stepper.currentPosition() + distance); free(args[0]); free(args[1]); - SERIAL_PORT.println("Stepstick started"); + comPort->println("Stepstick started"); moving = true; } @@ -55,7 +56,7 @@ void stepstick_loop() if (stepper.distanceToGo() == 0 && moving) { moving = false; - SERIAL_PORT.println("Stepstick stopped"); + comPort->println("Stepstick stopped"); } } diff --git a/test/test_argument_parsing.cpp b/test/test_argument_parsing.cpp index f8b01ba..f7c5e68 100644 --- a/test/test_argument_parsing.cpp +++ b/test/test_argument_parsing.cpp @@ -70,9 +70,9 @@ void test_multiple_argument_parsing(void) void setup() { - Serial.begin(115200); + comPort->begin(115200); // NOTE!!! Wait for >2 secs - // if board doesn't support software reset via Serial.DTR/RTS + // if board doesn't support software reset via comPort->DTR/RTS delay(2000); UNITY_BEGIN(); // IMPORTANT LINE! //bizzare string issues caused this to fail in specific sequences, so we run it multiple times From c38c0b71954a304a1060018c89fb6b34cf2ff425 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 12:07:47 +0000 Subject: [PATCH 04/19] Echo back invalid commands This was really helpful when debugging dodgy serial connections - and shouldn't hurt anything in normal operations. --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index fc56122..2aaa915 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,7 +74,9 @@ void handle_command(String received_command) } #ifdef HELP - comPort->println(F("Type 'help' for a list of commands.")); + comPort->print(F("Command not recognised: '")); + comPort->print(received_command); + comPort->println(F("', Type 'help' for a list of commands.")); #else comPort->println(F("Invalid command")); #endif From 40439a0ead2e3ec9eed83c144c8907d61cabf154 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 12:12:21 +0000 Subject: [PATCH 05/19] Remove ifndef This appears to be unterminated - I don't understand how it compiled before, but I'm removing it to get things working again. --- src/main.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2aaa915..84f36b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -135,7 +135,6 @@ void get_modules(String) comPort->println("--END--"); } -#ifndef UNIT_TEST void setup() { // initialise serial port @@ -145,11 +144,11 @@ void setup() #endif comPort = (HardwareSerial*) &PRIMARY_SERIAL_PORT; - PRIMARY_SERIAL_PORT->begin(115200); + PRIMARY_SERIAL_PORT.begin(115200); while (!PRIMARY_SERIAL_PORT) delay(1); #if defined(SECONDARY_SERIAL_PORT) - SECONDARY_SERIAL_PORT->begin(115200); + SECONDARY_SERIAL_PORT.begin(115200); while (!SECONDARY_SERIAL_PORT) delay(1); #endif From 3e4b3e97eb6e6b97abea30ee353e2d3310920082 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 12:17:45 +0000 Subject: [PATCH 06/19] Store current position in EEPROM Previously, we held our position in EEPROM so it persisted even across power cycles. This seems to have been lost, so I'm restoring it. --- src/modules/stage/stage.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/stage/stage.cpp b/src/modules/stage/stage.cpp index c228c86..fb84138 100644 --- a/src/modules/stage/stage.cpp +++ b/src/modules/stage/stage.cpp @@ -12,12 +12,13 @@ // The array below has 3 stepper objects, for X,Y,Z respectively const uint8_t n_motors = STAGE_N_MOTORS; +const uint8_t stage_position_eeprom = 0; long min_step_delay = -1; -const uint8_t min_step_delay_eeprom = sizeof(long) * n_motors; +const uint8_t min_step_delay_eeprom = stage_position_eeprom + sizeof(long) * n_motors; long ramp_time = -1; -const uint8_t ramp_time_eeprom = sizeof(long) * (n_motors + 1); -const uint8_t axis_max_eeprom = sizeof(long) * (n_motors + 2); -const uint8_t non_blocking_moves_eeprom = sizeof(long) * (n_motors + 3); +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]; @@ -181,6 +182,7 @@ void stage_loop() } } } + EEPROM.put(0, current_pos); // ensure our position is held in EEPROM so it persists if (!stage_moving && notify_on_stop) { From 28f7b538f0b8c44542972db9b91ff7c245e8292c Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 12:31:34 +0000 Subject: [PATCH 07/19] Enable EEPROM support for pico and ATMEGA boards --- src/boards.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/boards.h b/src/boards.h index 42699c1..c0ad7fe 100644 --- a/src/boards.h +++ b/src/boards.h @@ -31,6 +31,7 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define SUPPORT_EEPROM #elif defined(BOARD_SANGABOARDv2) #define BOARD_STRING "Sangaboard v0.2" #define WIRING_MOTOR_X 13, 12, 11, 10 @@ -43,6 +44,7 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define SUPPORT_EEPROM #elif defined(BOARD_CUSTOM_BLUEPILL) #define BOARD_STRING "Custom Bluepill board" #define WIRING_MOTOR_X PB11, PB10, PB1, PB0 @@ -70,6 +72,7 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define SUPPORT_EEPROM #elif defined(BOARD_SANGABOARDv5) #define BOARD_STRING "Sangaboard v0.5" #define WIRING_MOTOR_X 2, 3, 4, 5 @@ -94,6 +97,7 @@ #define WIRING_PWM_LEDS {25, 29} #define WIRING_CC_LED 24 //TODO: 4th motor support + #define SUPPORT_EEPROM #elif defined(BOARD_CUSTOM_ESP32) #define BOARD_STRING "Custom ESP32 board" #define WIRING_MOTOR_X 34, 32, 25, 27 @@ -122,4 +126,5 @@ #define PRIMARY_SERIAL_PORT Serial #define PWM_NUM 0 #define WIRING_PWM_LEDS {} + #define SUPPORT_EEPROM #endif \ No newline at end of file From f0ec05cc2030558c349610c8fb5dc01cd85d58fd Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Wed, 22 Feb 2023 17:09:51 +0000 Subject: [PATCH 08/19] Initialise EEPROM and save periodically This should be less of a disaster for the emulated EEPROM on the pico, but could use some finesse --- src/main.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/main.h | 1 + 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 84f36b2..2f4eb95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,11 @@ #include #include #include +#ifdef SUPPORT_EEPROM +#include +#else +#include "dummyEEPROM.h" +#endif #ifdef HELP #include "modules/help/help.h" @@ -54,6 +59,7 @@ uint8_t registered_loop_fn_count = 0; const Command core_commands[] = { {"version", get_version}, {"list_modules", get_modules}, + {"eeprom_last_commit", get_eeprom_last_commit}, END_COMMAND}; void register_module(const Command commands[], void (*loop_fn)(void)) @@ -135,8 +141,42 @@ void get_modules(String) comPort->println("--END--"); } +#if defined(SUPPORT_EEPROM) && defined(MCU_PICO) +unsigned long eeprom_last_commit; +void eeprom_commit_at_interval(unsigned long interval) +{ + unsigned long next_commit = eeprom_last_commit + interval; + unsigned long now = millis(); + // millis() can overflow - this if statement will trigger + // every `interval` accounting for overflows. + if ( (now > next_commit && next_commit > eeprom_last_commit) // no overflow + || (now > next_commit && now < eeprom_last_commit)) // overflow + { + EEPROM.commit(); + eeprom_last_commit = millis(); + } +} +#endif + +void get_eeprom_last_commit(String) +{ +#if defined(SUPPORT_EEPROM) && defined(MCU_PICO) + comPort->println(eeprom_last_commit); +#else + comPort->println("Not using pico"); +#endif + return; +} + void setup() { + #if defined(SUPPORT_EEPROM) && defined(MCU_PICO) + // On the Pico, EEPROM is emulated. + // This means we need to explicitly enable it, and also + // call EEPROM.commit() periodically to persist it to flash. + EEPROM.begin(256); + eeprom_last_commit = millis(); + #endif // initialise serial port #ifdef WIRING_SERIAL_TX Serial2.setTX(WIRING_SERIAL_TX); @@ -186,17 +226,26 @@ void setup() void loop() { + bool bytesAvailable = false; if (PRIMARY_SERIAL_PORT.available()) { comPort = (HardwareSerial*) &PRIMARY_SERIAL_PORT; - handle_command(comPort->readStringUntil('\n')); + bytesAvailable = true; } #if defined(SECONDARY_SERIAL_PORT) - if (SECONDARY_SERIAL_PORT.available()) + else if (SECONDARY_SERIAL_PORT.available()) { comPort = (HardwareSerial*) &SECONDARY_SERIAL_PORT; + bytesAvailable = true; + } + if (bytesAvailable) + { handle_command(comPort->readStringUntil('\n')); } + #if defined(SUPPORT_EEPROM) && defined(MCU_PICO) + // On the Pico, emulated EEPROM is only saved to flash when we call commit() + eeprom_commit_at_interval(10000); // NB this only writes to flash if something has changed. + #endif for (int i = 0; i < registered_loop_fn_count; i++) registered_loop_functions[i](); diff --git a/src/main.h b/src/main.h index 2ca26a8..2b74b83 100644 --- a/src/main.h +++ b/src/main.h @@ -29,5 +29,6 @@ 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); #define MAIN_H #endif \ No newline at end of file From 8a18c1444cb5ecb37352c415ca9a0bddfd73fb38 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 28 Feb 2023 10:23:24 +0000 Subject: [PATCH 09/19] Fix precompiler directives I mistakenly deleted an ifndef in 40439a0ead2e3ec9eed83c144c8907d61cabf154 thinking that it wasn't matched by an #endif. This change reverts that error, and it builds properly again. --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 2f4eb95..3dacad9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -168,6 +168,7 @@ void get_eeprom_last_commit(String) return; } +#ifndef UNIT_TEST // don't define setup() and loop() for unit testing void setup() { #if defined(SUPPORT_EEPROM) && defined(MCU_PICO) @@ -238,6 +239,7 @@ void loop() comPort = (HardwareSerial*) &SECONDARY_SERIAL_PORT; bytesAvailable = true; } + #endif if (bytesAvailable) { handle_command(comPort->readStringUntil('\n')); @@ -250,4 +252,4 @@ void loop() for (int i = 0; i < registered_loop_fn_count; i++) registered_loop_functions[i](); } -#endif \ No newline at end of file +#endif //ndef UNIT_TEST \ No newline at end of file From d9e7c1429a2e7695c306980d6964de14aa346fd0 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 28 Feb 2023 10:30:37 +0000 Subject: [PATCH 10/19] Simplified configuration I've moved SUPPORT_EEPROM into boards.h because that seemed simplest. I've removed a bit of logic related to serial ports, because we now just listen on both if there is more than one, using PRIMARY_SERIAL_PORT and SECONDARY_SERIAL_PORT in boards.h. I've deleted the `picohw` environment because it's no longer necessary. --- platformio.ini | 8 -------- src/boards.h | 9 +++------ src/config.h | 10 ---------- 3 files changed, 3 insertions(+), 24 deletions(-) diff --git a/platformio.ini b/platformio.ini index 3b79324..d468130 100644 --- a/platformio.ini +++ b/platformio.ini @@ -64,14 +64,6 @@ build_flags = -D MCU_ESP32 monitor_filters = esp32_exception_decoder build_type = debug -[env:picohw] -platform = https://github.com/maxgerhardt/platform-raspberrypi.git -board = pico -framework = arduino -board_build.core = earlephilhower -upload_port = /media/filip/RPI-RP2 -build_flags = -D MCU_PICO -D HW_SERIAL - [env:pico] platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = pico diff --git a/src/boards.h b/src/boards.h index c0ad7fe..a5f2807 100644 --- a/src/boards.h +++ b/src/boards.h @@ -70,6 +70,7 @@ #define WIRING_ADDITIONAL_STEPPERS = {{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 @@ -87,12 +88,8 @@ #define WIRING_STEPSTICK {23,22,1} //enable, step, dir todo: move to boards.h #define WIRING_SERIAL_TX 8 #define WIRING_SERIAL_RX 9 - #ifdef USB_SERIAL - #define PRIMARY_SERIAL_PORT Serial - #else - #define PRIMARY_SERIAL_PORT Serial - #define SECONDARY_SERIAL_PORT Serial2 - #endif + #define PRIMARY_SERIAL_PORT Serial + #define SECONDARY_SERIAL_PORT Serial2 #define PWM_NUM 2 #define WIRING_PWM_LEDS {25, 29} #define WIRING_CC_LED 24 diff --git a/src/config.h b/src/config.h index 92b1f6e..190f448 100644 --- a/src/config.h +++ b/src/config.h @@ -3,12 +3,6 @@ #define BOARD_AUTO //#define BOARD_SANGABOARDv5 - //if HW_SERIAL is not defined, we'll use USB serial communication - //the details are board specific and configured in boards.h - #ifndef HW_SERIAL - #define USB_SERIAL - #endif - #include //defines wiring //general settings @@ -44,9 +38,5 @@ #define LIGHT_SENSOR_ADAFRUIT_TSL2591 //#define LIGHT_SENSOR_ADAFRUIT_ADS1115 - //config for non-standard platforms - #ifndef MCU_PICO - #define SUPPORT_EEPROM - #endif #define CONFIGURED #endif \ No newline at end of file From 4a8481a2d3e54fcfea956dc39f292bb4632e5aef Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 28 Feb 2023 10:35:05 +0000 Subject: [PATCH 11/19] Save pico firmware as an artifact In the future, we could put this on the build server. --- .gitlab-ci.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d6c5426..72bafda 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,6 +15,12 @@ stages: before_script: - "pip install -U platformio" -job: +build: stage: build - script: "pio run -e nano -e leonardo -e pico -e bluepill -e esp32" \ No newline at end of file + script: + - "pio run -e nano -e leonardo -e pico -e bluepill -e esp32" + - mkdir -p builds + - cp .pio/build/pico/firmware.uf2 builds/ + artifacts: + paths: + - builds/* \ No newline at end of file From 54c34683c0553d47642222a224b6051dffba2021 Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 1 Mar 2023 04:52:06 +0000 Subject: [PATCH 12/19] Fix serial port switching, archive firmware.elf --- .gitlab-ci.yml | 3 ++- src/main.cpp | 13 +++++++------ test/test_argument_parsing.cpp | 5 +++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 72bafda..947eaff 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -17,10 +17,11 @@ before_script: build: stage: build - script: + script: - "pio run -e nano -e leonardo -e pico -e bluepill -e esp32" - mkdir -p builds - cp .pio/build/pico/firmware.uf2 builds/ + - cp .pio/build/pico/firmware.elf builds/ artifacts: paths: - builds/* \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3dacad9..6524b19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -186,13 +186,14 @@ void setup() comPort = (HardwareSerial*) &PRIMARY_SERIAL_PORT; PRIMARY_SERIAL_PORT.begin(115200); - while (!PRIMARY_SERIAL_PORT) - delay(1); - #if defined(SECONDARY_SERIAL_PORT) - SECONDARY_SERIAL_PORT.begin(115200); - while (!SECONDARY_SERIAL_PORT) - delay(1); + #ifdef SECONDARY_SERIAL_PORT + SECONDARY_SERIAL_PORT.begin(115200); + //wait for either port to be ready + while (!PRIMARY_SERIAL_PORT && !SECONDARY_SERIAL_PORT) + #else + while (!PRIMARY_SERIAL_PORT) #endif + delay(1); register_module(core_commands, NULL); diff --git a/test/test_argument_parsing.cpp b/test/test_argument_parsing.cpp index f7c5e68..e923cf5 100644 --- a/test/test_argument_parsing.cpp +++ b/test/test_argument_parsing.cpp @@ -9,7 +9,7 @@ void test_single_argument_parsing(void) String all_args = F("3123.123asdf"); char * args[1]; uint8_t ret = 0; - + ret = parse_arguments(args, all_args, 1); TEST_ASSERT_EQUAL_STRING(expected_single, args[0]); TEST_ASSERT_EQUAL(1, ret); @@ -70,7 +70,8 @@ void test_multiple_argument_parsing(void) void setup() { - comPort->begin(115200); + //TODO: support serial selection in tests + Serial.begin(115200); // NOTE!!! Wait for >2 secs // if board doesn't support software reset via comPort->DTR/RTS delay(2000); From 225e208eae31c7d1605201be7f3f6ab1d6c053b5 Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 1 Mar 2023 04:56:41 +0000 Subject: [PATCH 13/19] illumination: use comPort for test output --- src/modules/illumination/illumination.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/modules/illumination/illumination.cpp b/src/modules/illumination/illumination.cpp index aa54eba..83014f8 100644 --- a/src/modules/illumination/illumination.cpp +++ b/src/modules/illumination/illumination.cpp @@ -98,13 +98,13 @@ void cc_test() for (float v = 0; v <= 1; v+=0.02) { - Serial.print(v); - Serial.print(" "); + comPort->print(v); + comPort->print(" "); cc_set_value(v); - Serial.print(" "); + comPort->print(" "); delay(2000); float current = multi_read(A0, 1000)/4095.0*3.3; - Serial.println(current*1000); + comPort->println(current*1000); } cc_set_value(0); @@ -156,7 +156,7 @@ void cc_set_value(float val) stepd = tdown; } - //step up + //step current uint8_t steps_done =0; for (uint8_t i=16; i!=setting; i+=dir) { @@ -166,7 +166,6 @@ void cc_set_value(float val) delayMicroseconds(td); steps_done++; } - Serial.print(steps_done); #else comPort->println("CC LED not supported"); #endif From 5c0d38f3b234e0fbb7f448322f41a771b00b43ac Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 8 Mar 2023 01:22:22 +0000 Subject: [PATCH 14/19] Illumination module fixes, report as module --- src/main.cpp | 11 +- src/modules/illumination/illumination.cpp | 125 +++++++++++++++++----- src/modules/illumination/illumination.h | 1 + test/test_argument_parsing.cpp | 2 +- 4 files changed, 110 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6524b19..95c1c53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,16 +2,16 @@ * Sangaboard firmware * * Refactored from bath_open_instrumentation_group/sangaboard/arduino_code - * + * * This firmware was written by * Richard Bowman * Julian Stirling * Boyko Vodenicharski * Filip Ayazi - * + * * Much of the code is based on older code written by * James Sharkey and Fergus Riche - * + * * Released under GPL v3, 2021 */ @@ -137,6 +137,9 @@ void get_modules(String) comPort->print(" soft"); #endif comPort->println(); +#endif +#if defined(ILLUMINATION) + comPort->println("Illumination:default"); #endif comPort->println("--END--"); } @@ -173,7 +176,7 @@ void setup() { #if defined(SUPPORT_EEPROM) && defined(MCU_PICO) // On the Pico, EEPROM is emulated. - // This means we need to explicitly enable it, and also + // This means we need to explicitly enable it, and also // call EEPROM.commit() periodically to persist it to flash. EEPROM.begin(256); eeprom_last_commit = millis(); diff --git a/src/modules/illumination/illumination.cpp b/src/modules/illumination/illumination.cpp index 83014f8..e35c1bc 100644 --- a/src/modules/illumination/illumination.cpp +++ b/src/modules/illumination/illumination.cpp @@ -8,23 +8,53 @@ #include "dummyEEPROM.h" #endif +const uint16_t eeprom_offset = 100; + const uint8_t pwm_led_pins[] = WIRING_PWM_LEDS; +const uint16_t cc_value_eeprom = eeprom_offset; +const uint16_t pwm_frequency_eeprom = cc_value_eeprom + sizeof(uint8_t); +const uint16_t pwm_values_eeprom = pwm_frequency_eeprom + sizeof(uint16_t); + +uint32_t pwm_frequency; +uint8_t cc_value; + +uint16_t pwm_values[PWM_NUM]; + + void illumination_pwm_freq(String command) { char * args[1]; - parse_arguments(args, command, 1); + uint8_t parsed = parse_arguments(args, command, 1); #ifdef MCU_PICO - analogWriteFreq(atol(args[0]));//32khz seems sensible - analogWriteRange(65535); + if (parsed > 0 && !(args[0] == "" || args[0][0] == '?')) + { + pwm_frequency = atol(args[0]); + EEPROM.put(pwm_frequency_eeprom, pwm_frequency); + analogWriteFreq(pwm_frequency); + analogWriteRange(65535); + } - comPort->print("Frequency set: "); - comPort->print(atol(args[0])); + comPort->print("PWM Frequency:"); + comPort->print(pwm_frequency); comPort->println("Hz"); #else comPort->println("Feature not supported"); #endif - free(args[0]); + if (parsed > 0) + free(args[0]); +} + +void illumination_channels(String command) +{ + comPort->print("CC:"); + #ifdef WIRING_CC_LED + comPort->print(1); + #else + comPort->print(0); + #endif + comPort->print(" PWM:"); + comPort->println(PWM_NUM); } void illumination_pwm(String command) @@ -32,7 +62,26 @@ void illumination_pwm(String command) #ifdef MCU_PICO //PWM frequency is shared between both PWM outputs char * args[2]; - parse_arguments(args, command, 2); + uint8_t parsed = parse_arguments(args, command, 2); + if (parsed > 0 && args[0][0] == '?') + { + comPort->print("PWM:"); + for(int i=0; iprint(pwm_values[i]/65535.0); + #else + comPort->print(pwm_values[i]/255.0); + #endif + comPort->print(";"); + } + comPort->println(""); + free(args[0]); + if (parsed == 2) + free(args[1]); + + return; + } uint8_t index = atoi(args[0]); if (index >= PWM_NUM) { @@ -45,26 +94,28 @@ void illumination_pwm(String command) digitalWrite(pwm_led_pins[index], LOW); } #ifdef MCU_PICO - uint32_t pwm_val = val * 65535; + uint16_t pwm_val = val * 65535; #else - uint8_t pwm_val = val*255; + uint8_t pwm_val = val * 255; #endif analogWrite(pwm_led_pins[index], pwm_val); - comPort->print("Illumination set"); + pwm_values[index] = pwm_val; + EEPROM.put(pwm_values_eeprom+index*sizeof(uint16_t), pwm_val); + comPort->print("PWM:"); + comPort->print(index); + comPort->print(":"); comPort->println(pwm_val); - free(args[0]); - free(args[1]); + if (parsed > 0) + free(args[0]); + if (parsed > 1) + free(args[1]); #endif } void illumination_setup() { - #ifdef MCU_PICO - analogWriteRange(65535); - analogWriteFreq(32000);//32khz seems sensible - #endif for (uint8_t i = 0; i < PWM_NUM; i++) { pinMode(pwm_led_pins[i], OUTPUT); @@ -72,15 +123,31 @@ void illumination_setup() } #ifdef WIRING_CC_LED + EEPROM.get(cc_value_eeprom, cc_value); pinMode(WIRING_CC_LED, OUTPUT); + if (cc_value > 0) + cc_set_value(cc_value/32.0); #endif + #ifdef MCU_PICO + EEPROM.get(pwm_frequency_eeprom, pwm_frequency); + if (pwm_frequency < 1) + pwm_frequency = 64000; + analogWriteRange(65535); + analogWriteFreq(pwm_frequency);//32khz seems sensible + #endif + + for(int i = 0; i < PWM_NUM; i++) + { + EEPROM.get(pwm_values_eeprom+i*sizeof(uint16_t), pwm_values[i]); + analogWrite(pwm_led_pins[i], pwm_values[i]); + } + register_module(illumination_commands, NULL); } #ifdef WIRING_CC_LED - -//these functions are useful testing +//these functions are useful for testing float multi_read(uint8_t pin, uint16_t reads) { float total = 0; @@ -114,11 +181,17 @@ void cc_test() void cc_set(String command) { char * args[1]; - parse_arguments(args, command, 1); - float val = atof(args[0]); + uint8_t parsed = parse_arguments(args, command, 1); + if (parsed > 0 && !(args[0] == "" || args[0][0] == '?')) + { + float val = atof(args[0]); + cc_set_value(val); + } - free(args[0]); - cc_set_value(val); + comPort->print("CC LED:"); + comPort->println(cc_value/32.0); + if (parsed > 0) + free(args[0]); } void cc_set_value(float val) @@ -130,6 +203,9 @@ void cc_set_value(float val) return; } + cc_value = 32 * val; + EEPROM.put(cc_value_eeprom, cc_value); + const uint8_t td = 3; // >= 1.5 us const uint8_t tup = 2; // 1-75 us const uint8_t tdown = 180; //180-300us @@ -173,7 +249,8 @@ void cc_set_value(float val) extern const Command illumination_commands[] = { {"led_pwm", illumination_pwm}, - {"led_freq", illumination_pwm_freq}, - {"led_cc_set", cc_set}, + {"led_channels", illumination_channels}, + {"led_frequency", illumination_pwm_freq}, + {"led_cc", cc_set}, END_COMMAND}; #endif \ No newline at end of file diff --git a/src/modules/illumination/illumination.h b/src/modules/illumination/illumination.h index 2e4ae70..47044ce 100644 --- a/src/modules/illumination/illumination.h +++ b/src/modules/illumination/illumination.h @@ -7,6 +7,7 @@ void illumination_pwm(String command); void illumination_pwm_freq(String command); +void illumination_channels(String command); void cc_set(String command); void cc_set_value(float val); diff --git a/test/test_argument_parsing.cpp b/test/test_argument_parsing.cpp index e923cf5..adee2ba 100644 --- a/test/test_argument_parsing.cpp +++ b/test/test_argument_parsing.cpp @@ -74,7 +74,7 @@ void setup() { Serial.begin(115200); // NOTE!!! Wait for >2 secs // if board doesn't support software reset via comPort->DTR/RTS - delay(2000); + delay(5000); UNITY_BEGIN(); // IMPORTANT LINE! //bizzare string issues caused this to fail in specific sequences, so we run it multiple times RUN_TEST(test_multiple_argument_parsing); From 13e9a2c5988ca2fc9cb6960bbef1f2ee752078e8 Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 8 Mar 2023 01:36:31 +0000 Subject: [PATCH 15/19] Undo version hack --- src/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.h b/src/config.h index 553566d..aefd73c 100644 --- a/src/config.h +++ b/src/config.h @@ -9,7 +9,7 @@ #define MAX_COMMANDS 50 #define MAX_MODULES 10 #define MAX_ARGUMENT_LENGTH 20 //used in argument parsing, takes up ram - #define VERSION_STRING "Sangaboard Firmware v0.5.0" + #define VERSION_STRING "Sangaboard Firmware v1.0.0-beta" #define DEBUG_ON //module choice From 51b5b670e6c6d2dcf277501db9bebb0362388f0f Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 8 Mar 2023 23:51:22 +0000 Subject: [PATCH 16/19] Handle bool defaults on emulated eeprom platform platforms (fixes #5) --- src/config.h | 1 + src/dummyEEPROM.h | 6 ++++-- src/main.cpp | 2 +- src/main.h | 15 +++++++++++++++ src/modules/illumination/illumination.cpp | 3 ++- src/modules/stage/stage.cpp | 2 +- 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/config.h b/src/config.h index aefd73c..36c15a6 100644 --- a/src/config.h +++ b/src/config.h @@ -11,6 +11,7 @@ #define MAX_ARGUMENT_LENGTH 20 //used in argument parsing, takes up ram #define VERSION_STRING "Sangaboard Firmware v1.0.0-beta" #define DEBUG_ON + #define EMULATED_EEPROM_SIZE 512 //module choice #define HELP diff --git a/src/dummyEEPROM.h b/src/dummyEEPROM.h index 442a610..1802f23 100644 --- a/src/dummyEEPROM.h +++ b/src/dummyEEPROM.h @@ -1,4 +1,5 @@ - +#ifndef dummyEEPROM_h +#define dummyEEPROM_h #include struct EEPROMClass @@ -21,4 +22,5 @@ struct EEPROMClass } }; -static EEPROMClass EEPROM; \ No newline at end of file +static EEPROMClass EEPROM; +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 95c1c53..ba48ed3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -178,7 +178,7 @@ void setup() // On the Pico, EEPROM is emulated. // This means we need to explicitly enable it, and also // call EEPROM.commit() periodically to persist it to flash. - EEPROM.begin(256); + EEPROM.begin(EMULATED_EEPROM_SIZE); eeprom_last_commit = millis(); #endif // initialise serial port diff --git a/src/main.h b/src/main.h index 2b74b83..140c14a 100644 --- a/src/main.h +++ b/src/main.h @@ -2,6 +2,11 @@ #include "config.h" #include #include +#ifdef SUPPORT_EEPROM +#include +#else +#include "dummyEEPROM.h" +#endif #ifdef DEBUG_ON #define D(x) comPort->println(x) #else @@ -22,6 +27,16 @@ struct Command inline bool check_end_command(Command c) { return strlen(c.command) > 0; } inline bool check_end_command(Command * c) {return strlen(c->command) > 0; } +inline bool read_eeprom_bool(uint16_t address, bool default_value) +{ + int8_t temp_value; + EEPROM.get(address, temp_value); + if (temp_value < 0) + return default_value; + + return temp_value != 0; +} + const Command end_command = END_COMMAND; uint8_t parse_arguments(char ** arguments, String, uint8_t); diff --git a/src/modules/illumination/illumination.cpp b/src/modules/illumination/illumination.cpp index e35c1bc..86ee9ab 100644 --- a/src/modules/illumination/illumination.cpp +++ b/src/modules/illumination/illumination.cpp @@ -140,7 +140,8 @@ void illumination_setup() for(int i = 0; i < PWM_NUM; i++) { EEPROM.get(pwm_values_eeprom+i*sizeof(uint16_t), pwm_values[i]); - analogWrite(pwm_led_pins[i], pwm_values[i]); + if (pwm_values[i] > 0) + analogWrite(pwm_led_pins[i], pwm_values[i]); } register_module(illumination_commands, NULL); diff --git a/src/modules/stage/stage.cpp b/src/modules/stage/stage.cpp index 0ee6324..ead2f53 100644 --- a/src/modules/stage/stage.cpp +++ b/src/modules/stage/stage.cpp @@ -55,7 +55,7 @@ void stage_setup() } //using non-blocking moves here to have blocking moves by default - EEPROM.get(non_blocking_moves_eeprom, non_blocking_moves); + non_blocking_moves = read_eeprom_bool(non_blocking_moves_eeprom, false); register_module(stage_commands, stage_loop); } From 1b3e25178343e489c15b2258288e1c6797c17e5b Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Thu, 9 Mar 2023 00:05:12 +0000 Subject: [PATCH 17/19] 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) From 11f0f12ee1b20911c10960a562f5aca1422eafff Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Thu, 9 Mar 2023 00:06:55 +0000 Subject: [PATCH 18/19] add get_system_status command --- src/main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index ba48ed3..6c5e692 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,6 +60,7 @@ const Command core_commands[] = { {"version", get_version}, {"list_modules", get_modules}, {"eeprom_last_commit", get_eeprom_last_commit}, + {"system_status", get_system_status}, END_COMMAND}; void register_module(const Command commands[], void (*loop_fn)(void)) @@ -171,6 +172,16 @@ void get_eeprom_last_commit(String) return; } +void get_system_status(String) +{ + comPort->println(F(BOARD_STRING)); + comPort->println(F(VERSION_STRING)); + #ifdef MCU_PICO + comPort->print(F("Heap remaining:")); + comPort->println(rp2040.getFreeHeap()); + #endif +} + #ifndef UNIT_TEST // don't define setup() and loop() for unit testing void setup() { @@ -199,7 +210,6 @@ void setup() delay(1); register_module(core_commands, NULL); - #ifdef HELP help_setup(); #endif From 3745e5740b3892cef47232cd1e9b95d0c7aa2038 Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Fri, 10 Mar 2023 00:49:05 +0000 Subject: [PATCH 19/19] Add missing board command --- src/main.cpp | 7 +++++++ src/main.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 6c5e692..ca0a283 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,6 +58,7 @@ uint8_t registered_loop_fn_count = 0; const Command core_commands[] = { {"version", get_version}, + {"board", get_board}, {"list_modules", get_modules}, {"eeprom_last_commit", get_eeprom_last_commit}, {"system_status", get_system_status}, @@ -118,6 +119,12 @@ void get_version(String) return; } +void get_board(String) +{ + comPort->println(F(BOARD_STRING)); + return; +} + void get_modules(String) { #if defined(LIGHTSENSOR) && defined(LIGHT_SENSOR_ADAFRUIT_TSL2591) diff --git a/src/main.h b/src/main.h index f492713..b4d380b 100644 --- a/src/main.h +++ b/src/main.h @@ -43,6 +43,7 @@ uint8_t parse_arguments(char ** arguments, String, uint8_t); extern void register_module(const Command commands[], void (*loop_fn)(void)); void get_version(String); +void get_board(String); void get_modules(String); void get_eeprom_last_commit(String); void get_system_status(String);