diff --git a/esp32/README.md b/esp32/README.md new file mode 100644 index 0000000..051ac24 --- /dev/null +++ b/esp32/README.md @@ -0,0 +1,23 @@ +This module includes an arduino sketch designed to run on the ESP32. + +The sketch takes simple input from serial (ESP32 microusb port), and converts the input to a duty cycle that can be used on servos. + +The protocol specification is as follows, assuming an array of bytes: + + +| Byte Number | Value Description | +| :---------- | ----------: | +| 0 | 0 if Calibrating a servo. Higher values indicates channel number to set duty cycle on. | +| 1 | If byte 0 = 0: number of servos to calibrate. Else, the new duty cycle value for the channel specified in byte 0. | + +The table below describes the byte format for calibrating a servo. This array of bytes can be repeated for the given number of servos in byte 1: + +| Byte Number | Value Description | +| :---------- | ----------: | +| 0 | Servo channel to set (this will be byte 0 when setting duty cycle, so don't use 0). | +| 1 | The pin number to setup | +| 2 | The frequency for the pin | + +The min/max pulse widths are hardcoded to 1000us/2000us respectively. + +At the end of each loop, the entire array will be read, to flush any data that may cause issues later. \ No newline at end of file diff --git a/esp32/esp.ino b/esp32/esp.ino new file mode 100644 index 0000000..428916b --- /dev/null +++ b/esp32/esp.ino @@ -0,0 +1,37 @@ +#include +#include "Servo.h" + +std::map servos; + +void setup() +{ + Serial.begin(115200); +} + +void setupServos(uint8_t *calibrationValues, uint8_t size) +{ + // We assume there are 3 bytes per servo. Ignore if there aren't. + if (size % 3 == 0) + { + for (int i = 0; i < size; i += 3) + { + servos.insert(std::make_pair(calibrationValues[i], new Servo())) + } + } +} + +void loop() +{ + uint8_t *header = new uint8_t[2]; + Serial.readBytes(header, 2); + + if (header[0] == 0) + { + uint8_t *calibration = new uint8_t[3 * header[1]]; + Serial.readBytes(calibration, header[1]); + setupServos(); + delete calibration; + } + + delete header; +} \ No newline at end of file