Initial commit: add stepper controller firmware
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct DeviceConfig {
|
||||
String name;
|
||||
String hostname;
|
||||
};
|
||||
|
||||
struct MotorConfig {
|
||||
long stepsPerRevolution;
|
||||
float maxSpeed;
|
||||
float acceleration;
|
||||
bool reverseDirection;
|
||||
bool holdPosition;
|
||||
};
|
||||
|
||||
struct WifiConfig {
|
||||
String ssid;
|
||||
String password;
|
||||
String apName;
|
||||
String apPassword;
|
||||
};
|
||||
|
||||
struct LimitConfig {
|
||||
bool enabled;
|
||||
int leftPin;
|
||||
int rightPin;
|
||||
bool activeHigh;
|
||||
};
|
||||
|
||||
struct AppConfig {
|
||||
DeviceConfig device;
|
||||
MotorConfig motor;
|
||||
WifiConfig wifi;
|
||||
LimitConfig limits;
|
||||
};
|
||||
|
||||
class ConfigManager {
|
||||
public:
|
||||
bool begin();
|
||||
bool load();
|
||||
bool save();
|
||||
void resetToDefaults();
|
||||
const AppConfig& get() const;
|
||||
AppConfig& mutableConfig();
|
||||
|
||||
private:
|
||||
AppConfig config_;
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AccelStepper.h>
|
||||
#include "config/ConfigManager.h"
|
||||
|
||||
enum class MotorState {
|
||||
Idle,
|
||||
Moving,
|
||||
Continuous,
|
||||
LimitLeft,
|
||||
LimitRight,
|
||||
Error
|
||||
};
|
||||
|
||||
class StepperController {
|
||||
public:
|
||||
StepperController();
|
||||
void begin(const MotorConfig& config);
|
||||
void update();
|
||||
void moveLeftRotations(float rotations);
|
||||
void moveRightRotations(float rotations);
|
||||
void continuousLeft();
|
||||
void continuousRight();
|
||||
void stop();
|
||||
void emergencyStop();
|
||||
void setSpeed(float maxSpeed);
|
||||
void setAcceleration(float acceleration);
|
||||
void setLimits(bool enabled, bool leftTriggered, bool rightTriggered);
|
||||
bool isMoving() const;
|
||||
long position();
|
||||
MotorState state() const;
|
||||
const char* stateName() const;
|
||||
|
||||
private:
|
||||
bool directionAllowed(int direction) const;
|
||||
void applyLimitState();
|
||||
long rotationsToSteps(float rotations) const;
|
||||
|
||||
AccelStepper stepper_;
|
||||
long stepsPerRevolution_ = 4096;
|
||||
float maxSpeed_ = 500.0f;
|
||||
bool reverseDirection_ = false;
|
||||
bool holdPosition_ = false;
|
||||
bool limitsEnabled_ = false;
|
||||
bool leftLimit_ = false;
|
||||
bool rightLimit_ = false;
|
||||
MotorState state_ = MotorState::Idle;
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "config/ConfigManager.h"
|
||||
|
||||
class NetworkManager {
|
||||
public:
|
||||
bool begin(const WifiConfig& config, const String& hostname);
|
||||
void update();
|
||||
void startAccessPoint(const WifiConfig& config, const String& hostname);
|
||||
bool isAccessPoint() const;
|
||||
bool isConnected() const;
|
||||
String modeName() const;
|
||||
String ipAddress() const;
|
||||
|
||||
private:
|
||||
bool accessPoint_ = false;
|
||||
unsigned long lastReconnectAttempt_ = 0;
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include "motor/StepperController.h"
|
||||
|
||||
class OtaManager {
|
||||
public:
|
||||
void begin(const String& hostname, StepperController& motor);
|
||||
void update();
|
||||
void handleUpload(ESP8266WebServer& server, HTTPUpload& upload, StepperController& motor);
|
||||
bool isUpdating() const;
|
||||
|
||||
private:
|
||||
bool updating_ = false;
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include "config/ConfigManager.h"
|
||||
#include "motor/StepperController.h"
|
||||
#include "network/NetworkManager.h"
|
||||
#include "ota/OtaManager.h"
|
||||
|
||||
class WebServerManager {
|
||||
public:
|
||||
WebServerManager(ConfigManager& config, StepperController& motor, NetworkManager& network, OtaManager& ota);
|
||||
void begin();
|
||||
void update();
|
||||
|
||||
private:
|
||||
void registerRoutes();
|
||||
void handleRoot();
|
||||
void handleConfigPage();
|
||||
void handleStatus();
|
||||
void handleCommand();
|
||||
void handleConfigGet();
|
||||
void handleConfigPost();
|
||||
void handleFactoryReset();
|
||||
void handleNotFound();
|
||||
String renderPage() const;
|
||||
bool parseFloatArg(const String& name, float& value) const;
|
||||
|
||||
ESP8266WebServer server_{80};
|
||||
ConfigManager& config_;
|
||||
StepperController& motor_;
|
||||
NetworkManager& network_;
|
||||
OtaManager& ota_;
|
||||
};
|
||||
Reference in New Issue
Block a user