diff --git a/.gitignore b/.gitignore index 03f4a3c..759098a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .pio +src/wifi.h \ No newline at end of file diff --git a/lib/SyRen50/src/Syren50.cpp b/lib/SyRen50/src/Syren50.cpp new file mode 100644 index 0000000..c60abc0 --- /dev/null +++ b/lib/SyRen50/src/Syren50.cpp @@ -0,0 +1,39 @@ +#include "Arduino.h" +#include +#include "Syren50.h" + +void Syren50::init() { + Serial1.begin(9600); + delay(1000); // Give about 1 second to boot up, per datasheet + Serial.write(0b10101010); +} + +void Syren50::command(uint8_t command, uint8_t data) { + Serial1.write(0x80); // Address + Serial1.write(command); // Command + Serial1.write(data); + Serial1.write((0x80 + command + data) & 0b01111111); // Checksum + Serial1.flush(); +} + +void Syren50::forwards(uint8_t speed) { + command(0x00, speed); +} + +void Syren50::reverse(uint8_t speed) { + command(0x01, speed); +} + +void Syren50::move(int8_t direction, uint8_t speed) { + if (direction == 1) { + forwards(speed); + } else if (direction == -1) { + reverse(speed); + } else { + forwards(0); + } +} + +void Syren50::minInputVoltage(float voltage) { + command(0x03, (uint8_t)((voltage - 6.0) * 5.0)); +} diff --git a/lib/SyRen50/src/Syren50.h b/lib/SyRen50/src/Syren50.h new file mode 100644 index 0000000..94f956d --- /dev/null +++ b/lib/SyRen50/src/Syren50.h @@ -0,0 +1,18 @@ +#ifndef SYREN50_H +#define SYREN50_H + +#include + +class Syren50 { +public: + void init(); + void forwards(uint8_t speed); + void reverse(uint8_t speed); + void move(int8_t direction, uint8_t speed); + void minInputVoltage(float voltage); + + private: + void command(uint8_t command, uint8_t data); +}; + +#endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index ebc510b..c36403d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -7,3 +7,18 @@ ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html + + +[platformio] +default_envs = esp12e +name = WiFiTrainReceiver + +[env:esp12e] +platform = espressif8266 +framework = arduino +board = esp12e + +[env:nodemcuv2] +platform = espressif8266 +framework = arduino +board = nodemcuv2 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..da4cc4b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,39 @@ +#include "Arduino.h" +#include "wifi.h" +#include +#include +#include "SyRen50.h" +using namespace std; + +Syren50 motor; + +void setup() { + Serial.begin(9600); + // delay required for motor controller boot-up + delay(1000); + motor.init(); +} + +float throttle = 0.0f; +uint8_t direction = 0; +bool horn = false; +bool bell = false; +bool lights = false; + +uint8_t last_direction = 0; +float last_throttle = 0.0f; + +void inertia(float throttle, uint8_t direction, float &inertial_throttle, uint8_t &inertial_direction) { + inertial_throttle = throttle; + inertial_direction = direction; +} + +float motor_throttle; +uint8_t motor_direction; + +void loop() { + //get_control_inputs(throttle, direction, horn, bell, lights); + inertia(throttle, direction, motor_throttle, motor_direction); + motor.move(motor_direction, motor_throttle); + delay(1); +} \ No newline at end of file