Moved motor controller code out of handheld controller code

This commit is contained in:
Jared Dunbar 2023-02-19 00:01:02 -05:00
parent c59a99c8da
commit 6a9fad25e4
3 changed files with 3 additions and 81 deletions

View File

@ -1,39 +0,0 @@
#include "Arduino.h"
#include <stdint.h>
#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));
}

View File

@ -1,18 +0,0 @@
#ifndef SYREN50_H
#define SYREN50_H
#include <stdint.h>
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

View File

@ -2,49 +2,28 @@
#include "screen.h" #include "screen.h"
#include "wifi.h" #include "wifi.h"
#include "controls.h" #include "controls.h"
#include "Syren50.h"
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <string> #include <string>
using namespace std; using namespace std;
Syren50 motor;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
displayInit(); displayInit();
controlsInit(); controlsInit();
WiFi.softAP(WIFI_SSID, WIFI_PASS); WiFi.softAP(WIFI_SSID, WIFI_PASS);
displayWiFiInformation(WIFI_SSID, WIFI_PASS); displayWiFiInformation(WIFI_SSID, WIFI_PASS);
delay(1000);
motor.init();
} }
float throttle = 0.0f; float throttle = 0.0f;
int direction = 0; uint8_t direction = 0;
bool horn = false; bool horn = false;
bool bell = false; bool bell = false;
bool lights = false; bool lights = false;
int last_direction = 0;
float last_throttle = 0.0f;
void inertia(float throttle, int direction, float &inertial_throttle, int &inertial_direction) {
inertial_throttle = throttle;
inertial_direction = direction;
}
void loop() { void loop() {
getControlsState(throttle, direction, horn, bell, lights); getControlsState(throttle, direction, horn, bell, lights);
displayControlStatus(throttle, direction, horn, bell, lights);
float motor_throttle; //sendControlOutputs(throttle, direction, horn, bell, lights);
int motor_direction;
inertia(throttle, direction, motor_throttle, motor_direction);
displayControlStatus(motor_throttle, motor_direction, horn, bell, lights);
motor.move(motor_direction, motor_throttle * 127.0f);
delay(1); delay(1);
} }