Changed to using ESP32
This commit is contained in:
parent
383581f454
commit
bb6e0c75da
|
|
@ -2,18 +2,22 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "Syren50.h"
|
#include "Syren50.h"
|
||||||
|
|
||||||
|
#define RXD2 16
|
||||||
|
#define TXD2 17
|
||||||
|
|
||||||
void Syren50::init() {
|
void Syren50::init() {
|
||||||
Serial1.begin(9600);
|
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
|
||||||
delay(1000); // Give about 1 second to boot up, per datasheet
|
delay(1000); // Give about 1 second to boot up, per datasheet
|
||||||
Serial.write(0b10101010);
|
Serial2.write(0b10101010);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Syren50::command(uint8_t command, uint8_t data) {
|
void Syren50::command(uint8_t command, uint8_t data) {
|
||||||
Serial1.write(0x80); // Address
|
Serial2.write(0x80); // Address
|
||||||
Serial1.write(command); // Command
|
Serial2.write(command); // Command
|
||||||
Serial1.write(data);
|
Serial2.write(data);
|
||||||
Serial1.write((0x80 + command + data) & 0b01111111); // Checksum
|
Serial2.write((0x80 + command + data) & 0b01111111); // Checksum
|
||||||
Serial1.flush();
|
Serial2.flush();
|
||||||
|
delay(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Syren50::forwards(uint8_t speed) {
|
void Syren50::forwards(uint8_t speed) {
|
||||||
|
|
|
||||||
|
|
@ -10,15 +10,10 @@
|
||||||
|
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
default_envs = esp12e
|
default_envs = esp32dev
|
||||||
name = WiFiTrainReceiver
|
name = WiFiTrainReceiver
|
||||||
|
|
||||||
[env:esp12e]
|
[env:esp32dev]
|
||||||
platform = espressif8266
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = esp12e
|
board = esp32dev
|
||||||
|
|
||||||
[env:nodemcuv2]
|
|
||||||
platform = espressif8266
|
|
||||||
framework = arduino
|
|
||||||
board = nodemcuv2
|
|
||||||
35
src/main.cpp
35
src/main.cpp
|
|
@ -1,39 +1,54 @@
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "wifi.h"
|
#include <WiFi.h>
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "SyRen50.h"
|
#include "Syren50.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Syren50 motor;
|
Syren50 motor;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(115200);
|
||||||
// delay required for motor controller boot-up
|
// delay required for motor controller boot-up
|
||||||
delay(1000);
|
delay(1000);
|
||||||
motor.init();
|
motor.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
float throttle = 0.0f;
|
float throttle = 0.0f;
|
||||||
uint8_t direction = 0;
|
int8_t direction = 0;
|
||||||
bool horn = false;
|
bool horn = false;
|
||||||
bool bell = false;
|
bool bell = false;
|
||||||
bool lights = false;
|
bool lights = false;
|
||||||
|
|
||||||
uint8_t last_direction = 0;
|
int8_t last_direction = 0;
|
||||||
float last_throttle = 0.0f;
|
float last_throttle = 0.0f;
|
||||||
|
|
||||||
void inertia(float throttle, uint8_t direction, float &inertial_throttle, uint8_t &inertial_direction) {
|
void inertia(float throttle, int8_t direction, float &inertial_throttle, int8_t &inertial_direction) {
|
||||||
inertial_throttle = throttle;
|
inertial_throttle = throttle;
|
||||||
inertial_direction = direction;
|
inertial_direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
float motor_throttle;
|
float motor_throttle;
|
||||||
uint8_t motor_direction;
|
int8_t motor_direction;
|
||||||
|
|
||||||
|
void get_control_inputs(float &throttle, int8_t &direction) {
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
if (Serial.peek() == 'c') {
|
||||||
|
Serial.read();
|
||||||
|
Serial.println("Init motor");
|
||||||
|
motor.init();
|
||||||
|
} else {
|
||||||
|
int recvd = Serial.parseInt();
|
||||||
|
throttle = (float) recvd / 100.0;
|
||||||
|
Serial.println(throttle);
|
||||||
|
direction = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
//get_control_inputs(throttle, direction, horn, bell, lights);
|
get_control_inputs(throttle, direction);
|
||||||
inertia(throttle, direction, motor_throttle, motor_direction);
|
inertia(throttle, direction, motor_throttle, motor_direction);
|
||||||
motor.move(motor_direction, motor_throttle);
|
Serial.println(motor_throttle);
|
||||||
|
motor.move(motor_direction, (uint8_t) (motor_throttle * 127.0));
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue