diff --git a/growme/.vscode/settings.json b/growme/.vscode/settings.json index 8f5c4f6..df9ebe5 100644 --- a/growme/.vscode/settings.json +++ b/growme/.vscode/settings.json @@ -5,6 +5,7 @@ "*.tpl": "php", "*.phtml": "php", "wifi.h": "c", - "wifista.h": "c" + "wifista.h": "c", + "*.ipp": "cpp" } } diff --git a/growme/platformio.ini b/growme/platformio.ini index 6602d7a..40875eb 100644 --- a/growme/platformio.ini +++ b/growme/platformio.ini @@ -14,4 +14,5 @@ framework = arduino, espidf monitor_speed = 115200 [env:esp32dev] -board = esp32dev \ No newline at end of file +board = esp32dev +lib_deps = laurb9/StepperDriver@^1.4.0 diff --git a/growme/src/main.cpp b/growme/src/main.cpp index 47c94bb..58dcd61 100644 --- a/growme/src/main.cpp +++ b/growme/src/main.cpp @@ -1,54 +1,100 @@ #include #include #include -#include "sdkconfig.h" -#include #include +#include -void wifiScan() -{ - // WiFi.scanNetworks will return the number of networks found - int n = WiFi.scanNetworks(); - Serial.println("scan done"); - if (n == 0) - { - Serial.println("no networks found"); - } - else - { - Serial.print(n); - Serial.println(" networks found"); - for (int i = 0; i < n; ++i) - { - // Print SSID and RSSI for each network found - Serial.print(i + 1); - Serial.print(": "); - Serial.print(WiFi.SSID(i)); - Serial.print(" ("); - Serial.print(WiFi.RSSI(i)); - Serial.print(")"); - Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*"); - delay(10); - } - } - Serial.println(""); -} +#include "sdkconfig.h" +#include "A4988.h" -#if !CONFIG_AUTOSTART_ARDUINO +#define UP_BTN_PIN 2 +#define DOWN_BTN_PIN 15 + +// using a 200-step motor (most common) +#define MOTOR_STEPS 200 +// configure the pins connected +#define DIR 17 +#define STEP 16 + +A4988 stepper(MOTOR_STEPS, DIR, STEP); + +// TODO: for now use simple arduino based +// task structure adjust as needed afterwards void arduinoTask(void *pvParameter) { - // Set WiFi to station mode and disconnect from an AP if it was previously connected - WiFi.mode(WIFI_STA); - WiFi.disconnect(); + // setup serial COMs Serial.begin(115200); + + // configure button + pinMode(UP_BTN_PIN, INPUT); + pinMode(DOWN_BTN_PIN, INPUT); + + // configure motor + stepper.begin(120, 1); + + // motor movement direction + // -1 = counter clockwise + // 0 = nothing + // 1 = clockwise + int direction = 0; + + // track current movement of motor + uint motorWaitTimeMicros = 0; + + // time for RTOS to use when motor is not spinning + uint noActionIdleTime = pdMS_TO_TICKS(100); + + // pick large angle to rotate, such that motor will not stop between loop iterations + // and RTOS has time to do other things + int rotationAngle = 100 * 360; + + // add a delay for the MCU to apply setup delay(100); while (1) { - wifiScan(); + // parse movement direction + if (digitalRead(UP_BTN_PIN) == HIGH) + { + direction = 1; + } + else if (digitalRead(DOWN_BTN_PIN) == HIGH) + { + direction = -1; + } + else + { + direction = 0; + } - // Wait a bit before scanning again - delay(5000); + // start spinning motor if: + // - button pressed + // - no spin command was already started + if (direction == 0) + { + stepper.stop(); + } + else + { + stepper.startRotate(direction * rotationAngle); + } + + // motor control loop - send pulse and return how long to wait until next pulse + motorWaitTimeMicros = stepper.nextAction(); + if (motorWaitTimeMicros <= 0) + { + + vTaskDelay(noActionIdleTime); + } + else if (motorWaitTimeMicros >= 50) + { + vTaskDelay(pdMS_TO_TICKS(motorWaitTimeMicros / 1000)); + } + else + { + // give tiny amount for RTOS scheduling + vTaskDelay(20); + } } } @@ -57,22 +103,5 @@ extern "C" void app_main() // initialize arduino library before we start the tasks initArduino(); - xTaskCreate(&arduinoTask, "arduino_task", 4096, NULL, 5, NULL); + xTaskCreate(&arduinoTask, "arduino_task", 8192, NULL, 5, NULL); } -#else -void setup() -{ - // Set WiFi to station mode and disconnect from an AP if it was previously connected - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - Serial.begin(115200); - delay(100); -} - -void loop() -{ - wifiScan(); - // Wait a bit before scanning again - delay(5000); -} -#endif \ No newline at end of file