diff --git a/pousseseringue-arduino.cpp b/pousseseringue-arduino.cpp index 3ca8f5db320778b1d626ea2fcbf5a2506f5e9738..0286217979723e31c4c5ab267213f2a2bc49fd81 100644 --- a/pousseseringue-arduino.cpp +++ b/pousseseringue-arduino.cpp @@ -3,12 +3,9 @@ // Feather LCD // Feather motor controler -#if 0 -#define STASSID "laas-capture" -#define STAPSK "" -#else +#ifndef STASSID #define STASSID "laas-welcome" -#define STAPSK "WifiLAAS2018" // this password is wrong +#define STAPSK "WifiLAAS2022" // this password is wrong #endif #define NAME "PousseSeringue" @@ -19,7 +16,7 @@ #define BTNC 2 // gpio C adafruit feather oled 128x32 #define OLED_I2C 0x3c // adafruit feather oled 128x32 -#if 1 // 1 for laas board +#ifndef TESTBOARD // A4988 PINOUT #define DIR 15 // esp8266 gpio @@ -37,6 +34,10 @@ #endif // proto pinout +#ifndef BAUDRATE +#define BAUDRATE 9600 +#endif + // should this be configurable through a GUI ? #define MOTOR_STEPS 200 // 17HS19 #define MICROSTEPS_CONF 16 // hardware configuration: A4988's MS1,MS2,MS3 tied up @@ -50,10 +51,26 @@ #include <ESP8266WiFi.h> // internal, wifi library #include <DNSServer.h> // internal, access point mode + #if !CORE_MOCK + #include <SSD1306AsciiWire.h> // https://github.com/greiman/SSD1306Ascii -#include <AccelStepper.h> +SSD1306AsciiWire oled; + +#if USE_A4988 + +#include <A4988.h> // https://github.com/laurb9/StepperDriver +A4988 a4988(MOTOR_STEPS, DIR, STEP, SLEEP); + +#else + +#include <AccelStepper.h> // https://github.com/waspinator/AccelStepper +AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR); + +#endif + #endif // !CORE_MOCK + #include "Debouncer.h" // local, debouncer, short counter, long detector #include "syringe.h" #include "cli.h" @@ -68,10 +85,6 @@ millis_t nowMs; // to update on main loop(): "nowMs = millis();" Debouncer<nowMs> bA, bB, bC; -#if !CORE_MOCK -AccelStepper stepper(AccelStepper::DRIVER, STEP,DIR); -SSD1306AsciiWire oled; -#endif // !CORE_MOCK constexpr int DNS_PORT = 53; DNSServer dnsServer; @@ -155,7 +168,7 @@ void oledRedisplay () oled.printf("\nAPSSID & hostname:"); oled.printf("\n%s", ssid); break; - + default: oled.printf("?"); } @@ -185,7 +198,7 @@ void buttons (int shortp, int longp) Serial.printf("button change: short:%d long:%d\n", shortp, longp); rpming = 0; // anything stops rpming - + switch (mode) { case Modes::TURN: @@ -217,9 +230,11 @@ void buttons (int shortp, int longp) + + void setup() { - Serial.begin(9600); + Serial.begin(BAUDRATE); Serial.setDebugOutput(true); #if !CORE_MOCK Wire.begin(); @@ -228,16 +243,25 @@ void setup() delay(500); #if !CORE_MOCK + // initialize hardware + oled.begin(&OLED_HW, OLED_I2C); oled.setFont(System5x7); oled.setScrollMode(SCROLL_MODE_AUTO); oled.clear(); oled.println("Booting...\n"); -#endif // !CORE_MOCK -#if !CORE_MOCK + +#if USE_A4988 + a4988.begin(STEPPER_RPM); // some value is necessary + a4988.setEnableActiveState(LOW); + a4988.setMicrostep(MICROSTEPS_CONF); + a4988.enable(); +#else stepper.setMaxSpeed(10000); stepper.setAcceleration(10000); stepper.moveTo(200); +#endif + #endif // !CORE_MOCK uint8_t mac[6]; @@ -263,7 +287,7 @@ void setup() pinMode(BTNA, INPUT_PULLUP); pinMode(BTNB, INPUT_PULLUP); pinMode(BTNC, INPUT_PULLUP); - + oledRedisplay(); } @@ -271,7 +295,7 @@ void setup() void loop() { nowMs = millis(); - + #if CORE_MOCK bool changeA = false; bool changeB = false; @@ -289,7 +313,7 @@ void loop() // are not cleared until they are read int shortA = bA.shortCount(); bool longA = bA.longState(); - + buttons(shortA, longA); } @@ -305,26 +329,59 @@ void loop() { int shortB = bB.shortCount(); bool longB = bB.longState(); - + if (longB) changeMode(); (void)shortB; - + move = 0; } +#if !CORE_MOCK + // Stepper - #if !CORE_MOCK + +#if USE_A4988 + // MOVE THIS BLOCK INTO A TIMER/ISR + // (it is randomly slowed down by prints / web / wifi) + // (or? use A4988 RPM mode, related: a4988.begin(STEPPER_RPM);) + if (lastMoveMs - nowMs > (millis_t)stepDurationMs) + { + if (rpming && move < MOTOR_STEPS * MICROSTEPS_CONF && move > -(MOTOR_STEPS * MICROSTEPS_CONF)) + { + move += rpming * MOTOR_STEPS * MICROSTEPS_CONF; + //Serial.printf("feeding %d\n", move); + } + + if (move >= stepsPerDuration) + { + move -= stepsPerDuration; + a4988.move(stepsPerDuration); // forward revolution + } + if (move <= -stepsPerDuration) + { + move += stepsPerDuration; + a4988.move(-stepsPerDuration); // backward revolution + } + + lastMoveMs += stepDurationMs; + + } +#else + if (stepper.distanceToGo() == 0) { stepper.moveTo(-stepper.currentPosition()); Serial.println("Rotating Motor in opposite direction..."); } stepper.run(); - #endif // !CORE_MOCK + +#endif + +#endif // !CORE_MOCK + console.loop(Serial); webLoop(); - dnsServer.processNextRequest(); // access-point redirection } diff --git a/pousseseringue-arduino.ino.globals.h-template b/pousseseringue-arduino.ino.globals.h-template new file mode 100644 index 0000000000000000000000000000000000000000..731f06afb41187d2b229242766471ca7e52a8a29 --- /dev/null +++ b/pousseseringue-arduino.ino.globals.h-template @@ -0,0 +1,14 @@ + +// this file is included first with git version or from release 3.1 of the esp8266 arduino core + +#undef STASSID +#undef STAPSK + +#define STASSID "hackaton" +#define STAPSK "WifiLAAS2022" // changeme + +#define USE_A4988 1 + +#define TESTBOARD 0 + +#define BAUDRATE 74880 diff --git a/web.cpp b/web.cpp index b2b0794b03f37f23bac52961981caa58a16eb180..2b22eda88aff0f7a40edf0964cfdb3f8089ddfee 100644 --- a/web.cpp +++ b/web.cpp @@ -82,6 +82,7 @@ void selectExample(Control *sender, int value) { Serial.println(sender->value); } +uint8_t mock_read_uart(void); void webSetup () { @@ -134,7 +135,6 @@ void webSetup () * password, for example begin("ESPUI Control", "username", "password") */ - ESPUI.begin("ESPUI Control"); }