Refactoring of config file
This commit is contained in:
parent
7ad64c2e51
commit
afd4d20f23
@ -14,6 +14,7 @@ BraceWrapping:
|
||||
BeforeCatch: true
|
||||
BeforeElse: true
|
||||
PointerAlignment: Left
|
||||
ColumnLimit: 100
|
||||
|
||||
AlwaysBreakAfterReturnType: None
|
||||
PenaltyReturnTypeOnItsOwnLine: 1000000
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
include/settings.h
|
||||
include/settings.h
|
||||
include/config.h
|
@ -1,32 +1,32 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#define EEPROM_SIZE 64
|
||||
|
||||
class Config {
|
||||
class FlashConfig {
|
||||
public:
|
||||
typedef struct Data {
|
||||
std::array<char, 16> wifiSSID;
|
||||
std::array<char, 16> wifiPass;
|
||||
|
||||
Data &operator=(const Data &);
|
||||
Data& operator=(const Data&);
|
||||
} Data;
|
||||
|
||||
Config(){};
|
||||
FlashConfig(){};
|
||||
|
||||
void init();
|
||||
|
||||
void load();
|
||||
|
||||
void save();
|
||||
void save(Data &);
|
||||
void save(Data&);
|
||||
|
||||
void setSSID(String);
|
||||
void setWifiPass(String);
|
||||
|
||||
const Data &data();
|
||||
const Data& data();
|
||||
|
||||
void printConfig();
|
||||
|
||||
@ -34,5 +34,5 @@ private:
|
||||
Data _data;
|
||||
};
|
||||
|
||||
bool operator==(const Config::Data &a, const Config::Data &b);
|
||||
bool operator!=(const Config::Data &a, const Config::Data &b);
|
||||
bool operator==(const FlashConfig::Data& a, const FlashConfig::Data& b);
|
||||
bool operator!=(const FlashConfig::Data& a, const FlashConfig::Data& b);
|
@ -34,9 +34,9 @@ public:
|
||||
bool isNew;
|
||||
} Output;
|
||||
|
||||
virtual const Output &output() = 0;
|
||||
virtual const Output& output() = 0;
|
||||
|
||||
virtual void loop() = 0;
|
||||
|
||||
virtual void registerCallback(void (*callback)(const Output &)) = 0;
|
||||
virtual void registerCallback(void (*callback)(const Output&)) = 0;
|
||||
};
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include "IRemote.h"
|
||||
|
||||
#include "Vbat.h"
|
||||
|
||||
#include <OSCBundle.h>
|
||||
@ -8,19 +7,18 @@
|
||||
#include <OSCMessage.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
|
||||
#include <list>
|
||||
|
||||
class OSCRemote : public IRemote {
|
||||
public:
|
||||
//! Needs to be called in "Setup"
|
||||
OSCRemote(const Vbat &);
|
||||
OSCRemote(const Vbat&);
|
||||
|
||||
void loop();
|
||||
|
||||
const IRemote::Output &output() override;
|
||||
const IRemote::Output& output() override;
|
||||
|
||||
void registerCallback(void (*callback)(const IRemote::Output &)) override;
|
||||
void registerCallback(void (*callback)(const IRemote::Output&)) override;
|
||||
|
||||
private:
|
||||
//! Place this after WiFi.begin() in main
|
||||
@ -28,7 +26,7 @@ private:
|
||||
|
||||
IRemote::Output _output;
|
||||
|
||||
std::list<void (*)(const IRemote::Output &)> _callbacks;
|
||||
std::list<void (*)(const IRemote::Output&)> _callbacks;
|
||||
|
||||
const Vbat &_vbat;
|
||||
const Vbat& _vbat;
|
||||
};
|
@ -3,7 +3,6 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
class Leg {
|
||||
public:
|
||||
enum Joint {
|
||||
|
19
include/config.h.orig
Normal file
19
include/config.h.orig
Normal file
@ -0,0 +1,19 @@
|
||||
/*****************************************************
|
||||
* Build Configurations as debugging etc. *
|
||||
* *
|
||||
* Copy or renamed this file to "config.h" *
|
||||
* and edit it to your needs *
|
||||
* *
|
||||
****************************************************/
|
||||
|
||||
//! FlashConfig
|
||||
#define EEPROM_SIZE 64
|
||||
|
||||
//! OSCRemote
|
||||
//#define OSC_DEBUG // Enables CM prints if commented
|
||||
const unsigned int outPort = 9999;
|
||||
const unsigned int inPort = 8888;
|
||||
|
||||
//! Body
|
||||
//#define CTRL_INACTIVE
|
||||
#define SERVO_IIC_ADDR (0x40)
|
@ -1,12 +0,0 @@
|
||||
/*****************************************************
|
||||
* Configurations: *
|
||||
* *
|
||||
* Copy or renamed this file to "settings.h" *
|
||||
* and edit it to your needs *
|
||||
* *
|
||||
* The settings will be stored to the controller if *
|
||||
* different from the previous/current configuration *
|
||||
****************************************************/
|
||||
|
||||
const char* wifiSSID = "mySSID";
|
||||
const char* wifiPass = "myPass";
|
@ -1,27 +1,27 @@
|
||||
#include "config.h"
|
||||
#include "FlashConfig.h"
|
||||
|
||||
void Config::init() {
|
||||
void FlashConfig::init() {
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
}
|
||||
|
||||
void Config::setSSID(String ssid) {
|
||||
void FlashConfig::setSSID(String ssid) {
|
||||
strcpy(_data.wifiSSID.data(), ssid.c_str());
|
||||
};
|
||||
|
||||
void Config::setWifiPass(String pass) {
|
||||
void FlashConfig::setWifiPass(String pass) {
|
||||
strcpy(_data.wifiPass.data(), pass.c_str());
|
||||
};
|
||||
|
||||
void Config::load() {
|
||||
byte *pBuff = (byte *)(const void *)&_data;
|
||||
void FlashConfig::load() {
|
||||
byte* pBuff = (byte*)(const void*)&_data;
|
||||
for (size_t i = 0; i < sizeof(_data); ++i) {
|
||||
*pBuff++ = EEPROM.read(i);
|
||||
}
|
||||
Serial.println("EEPROM Loaded");
|
||||
};
|
||||
|
||||
void Config::save() {
|
||||
const byte *pBuff = (const byte *)(const void *)&_data;
|
||||
void FlashConfig::save() {
|
||||
const byte* pBuff = (const byte*)(const void*)&_data;
|
||||
for (size_t i = 0; i < sizeof(_data); ++i) {
|
||||
EEPROM.write(i, *pBuff++);
|
||||
}
|
||||
@ -29,29 +29,29 @@ void Config::save() {
|
||||
Serial.println("EEPROM Saved");
|
||||
};
|
||||
|
||||
void Config::save(Config::Data &data) {
|
||||
void FlashConfig::save(FlashConfig::Data& data) {
|
||||
_data = data;
|
||||
save();
|
||||
}
|
||||
|
||||
const Config::Data &Config::data() {
|
||||
const FlashConfig::Data& FlashConfig::data() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
void Config::printConfig() {
|
||||
void FlashConfig::printConfig() {
|
||||
Serial.printf("wifiSSID: %s\n", _data.wifiSSID.data());
|
||||
Serial.printf("wifiPass: %s\n", _data.wifiPass.data());
|
||||
}
|
||||
|
||||
Config::Data &Config::Data::operator=(const Config::Data &other) {
|
||||
FlashConfig::Data& FlashConfig::Data::operator=(const FlashConfig::Data& other) {
|
||||
this->wifiSSID = other.wifiSSID;
|
||||
this->wifiPass = other.wifiPass;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const Config::Data &a, const Config::Data &b) {
|
||||
bool operator==(const FlashConfig::Data& a, const FlashConfig::Data& b) {
|
||||
return (a.wifiSSID == b.wifiSSID && a.wifiPass == b.wifiPass);
|
||||
}
|
||||
bool operator!=(const Config::Data &a, const Config::Data &b) {
|
||||
bool operator!=(const FlashConfig::Data& a, const FlashConfig::Data& b) {
|
||||
return !(a == b);
|
||||
}
|
@ -1,19 +1,13 @@
|
||||
#include "OSCRemote.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
//! --- Start of onfiguration ---
|
||||
//#define DEBUG // Enables CM prints if commented
|
||||
|
||||
const unsigned int outPort = 9999;
|
||||
const unsigned int inPort = 8888;
|
||||
//! --- End of configuration ---
|
||||
|
||||
IRemote::Output *pOutput;
|
||||
IRemote::Output* pOutput;
|
||||
|
||||
namespace {
|
||||
|
||||
WiFiUDP *pUdp;
|
||||
WiFiUDP* pUdp;
|
||||
IPAddress remoteIP;
|
||||
|
||||
// Scaling
|
||||
@ -26,10 +20,15 @@ const float vxFactor = 0.1;
|
||||
const float vyFactor = 0.1;
|
||||
const float wFactor = 10;
|
||||
|
||||
void remoteMsgParser(OSCMessage &msg, int offset) {
|
||||
void remoteMsgParser(OSCMessage& msg, int offset) {
|
||||
std::array<char, 20> topic;
|
||||
msg.getAddress(topic.data());
|
||||
Serial.println(topic.data());
|
||||
#ifdef DEBUG_OSC
|
||||
Serial.printf("%s [%s, %s]\n",
|
||||
topic.data(),
|
||||
String(msg.getFloat(0)).c_str(),
|
||||
String(msg.getFloat(1)).c_str());
|
||||
#endif
|
||||
|
||||
if (!strcmp(topic.data(), "/att/xy")) {
|
||||
pOutput->attitude.pitch = msg.getFloat(0) * pitchFactor;
|
||||
@ -53,7 +52,7 @@ void remoteMsgParser(OSCMessage &msg, int offset) {
|
||||
}
|
||||
}
|
||||
|
||||
void remoteMsgParser(OSCMessage &msg) {
|
||||
void remoteMsgParser(OSCMessage& msg) {
|
||||
remoteMsgParser(msg, 0);
|
||||
}
|
||||
|
||||
@ -94,7 +93,7 @@ bool msgReceive() {
|
||||
msg.fill(pUdp->read());
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG_OSC
|
||||
Serial.printf("OSC packet from: %s\n", remoteIP.toString().c_str());
|
||||
#endif
|
||||
|
||||
@ -108,9 +107,8 @@ bool msgReceive() {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
#ifdef DEBUG
|
||||
Serial.printf("error: %s\n",
|
||||
getOSCErrorName(msg.getError()).c_str());
|
||||
#ifdef DEBUG_OSC
|
||||
Serial.printf("error: %s\n", getOSCErrorName(msg.getError()).c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@ -118,7 +116,7 @@ bool msgReceive() {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
OSCRemote::OSCRemote(const Vbat &vbat) : _vbat(vbat) {
|
||||
OSCRemote::OSCRemote(const Vbat& vbat) : _vbat(vbat) {
|
||||
init();
|
||||
}
|
||||
|
||||
@ -151,10 +149,10 @@ void OSCRemote::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
const IRemote::Output &OSCRemote::output() {
|
||||
const IRemote::Output& OSCRemote::output() {
|
||||
return _output;
|
||||
}
|
||||
|
||||
void OSCRemote::registerCallback(void (*callback)(const IRemote::Output &)) {
|
||||
void OSCRemote::registerCallback(void (*callback)(const IRemote::Output&)) {
|
||||
_callbacks.push_back(callback);
|
||||
}
|
53
src/body.cpp
53
src/body.cpp
@ -1,34 +1,31 @@
|
||||
#include "body.h"
|
||||
#include "Body.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <Adafruit_PWMServoDriver.h>
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define CTRL_INACTIVE
|
||||
|
||||
#define SERVO_IIC_ADDR (0x40)
|
||||
|
||||
// Depending on your servo make, the pulse width min and max may vary, you
|
||||
// want these to be as small/large as possible without hitting the hard stop
|
||||
// for max range. You'll have to tweak them as necessary to match the servos you
|
||||
// have!
|
||||
#define SERVOMIN 190 // This is the 'minimum' pulse length count (out of 4096)
|
||||
#define SERVOMAX 540 // This is the 'maximum' pulse length count (out of 4096)
|
||||
#define USMIN \
|
||||
#define USMIN \
|
||||
600 // This is the rounded 'minimum' microsecond length based on the minimum
|
||||
// pulse of 190
|
||||
#define USMAX \
|
||||
2400 // This is the rounded 'maximum' microsecond length based on the
|
||||
// maximum pulse of 540
|
||||
#define USMAX \
|
||||
2400 // This is the rounded 'maximum' microsecond length based on the
|
||||
// maximum pulse of 540
|
||||
#define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates
|
||||
|
||||
Adafruit_PWMServoDriver servoDriver = Adafruit_PWMServoDriver(0x40);
|
||||
|
||||
unsigned long freqWatchDog = 0;
|
||||
unsigned long SuppressScamperUntil =
|
||||
0; // if we had to wake up the servos, suppress the power hunger scamper
|
||||
// mode for a while
|
||||
unsigned long SuppressScamperUntil = 0; // if we had to wake up the servos, suppress the power
|
||||
// hunger scamper mode for a while
|
||||
|
||||
void resetServoDriver() {
|
||||
#ifndef CTRL_INACTIVE
|
||||
@ -57,20 +54,17 @@ void checkForServoSleep() {
|
||||
// beep(1200,100); // chirp to warn user of brown out on servo
|
||||
// controller
|
||||
SuppressScamperUntil =
|
||||
millis() +
|
||||
10000; // no scamper for you! (for 10 seconds because we ran out
|
||||
// of power, give the battery a bit of time for charge
|
||||
// migration and let the servos cool down)
|
||||
millis() + 10000; // no scamper for you! (for 10 seconds because we ran out
|
||||
// of power, give the battery a bit of time for charge
|
||||
// migration and let the servos cool down)
|
||||
}
|
||||
freqWatchDog = millis() + 100;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
double modifiedMap(
|
||||
double x, double in_min, double in_max, double out_min, double out_max) {
|
||||
double temp =
|
||||
(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
double modifiedMap(double x, double in_min, double in_max, double out_min, double out_max) {
|
||||
double temp = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
temp = (int)(4 * temp + .5);
|
||||
return (double)temp / 4;
|
||||
}
|
||||
@ -90,10 +84,8 @@ void Body::healthCheck() {
|
||||
}
|
||||
|
||||
Leg::Leg(uint8_t hipp, uint8_t knee)
|
||||
: _joints(
|
||||
{{Joint::Hipp, {.index = hipp, .trim = 0, .center = 0.0, .pos = 0}},
|
||||
{Joint::Knee,
|
||||
{.index = knee, .trim = 0, .center = 45.0, .pos = 0}}}) {
|
||||
: _joints({{Joint::Hipp, {.index = hipp, .trim = 0, .center = 0.0, .pos = 0}},
|
||||
{Joint::Knee, {.index = knee, .trim = 0, .center = 45.0, .pos = 0}}}) {
|
||||
}
|
||||
|
||||
void Leg::setTrim(Joint j, uint8_t angle) {
|
||||
@ -105,16 +97,11 @@ void Leg::setPos(Joint j, double angle) {
|
||||
//! servo
|
||||
|
||||
#ifndef CTRL_INACTIVE
|
||||
auto angleToPulse = [](double a) {
|
||||
return modifiedMap(a, -180.0, 180.0, SERVOMIN, SERVOMAX);
|
||||
};
|
||||
// servoDriver.setPWM(_joints[j].index, 0, angleToPulse(angle));
|
||||
// auto angleToPulse = [](double a) { return modifiedMap(a, -180.0, 180.0, SERVOMIN, SERVOMAX);
|
||||
// } servoDriver.setPWM(_joints[j].index, 0, angleToPulse(angle));
|
||||
|
||||
auto angleToMicroPulse = [](double a) {
|
||||
return modifiedMap(a, -180.0, 180.0, USMIN, USMAX);
|
||||
};
|
||||
servoDriver.writeMicroseconds(_joints[j].index,
|
||||
angleToMicroPulse(angle) + _joints[j].center);
|
||||
auto angleToMicroPulse = [](double a) { return modifiedMap(a, -180.0, 180.0, USMIN, USMAX); };
|
||||
servoDriver.writeMicroseconds(_joints[j].index, angleToMicroPulse(angle) + _joints[j].center);
|
||||
#endif
|
||||
|
||||
_joints[j].pos = angle;
|
||||
|
18
src/main.cpp
18
src/main.cpp
@ -1,21 +1,20 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
// Project includes
|
||||
#include "FlashConfig.h"
|
||||
#include "OSCRemote.h"
|
||||
#include "Vbat.h"
|
||||
#include "body.h"
|
||||
#include "config.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
//! --- Start of onfiguration ---
|
||||
const char *hostname = "Hexapod";
|
||||
const char* hostname = "Hexapod";
|
||||
//! --- End of configuration ---
|
||||
|
||||
Body body = Body::instance();
|
||||
Config config;
|
||||
FlashConfig config;
|
||||
std::unique_ptr<IRemote> remote;
|
||||
Vbat vbat;
|
||||
|
||||
@ -40,7 +39,7 @@ void connectWiFi() {
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void testRemoteCallback(const IRemote::Output &o) {
|
||||
void testRemoteCallback(const IRemote::Output& o) {
|
||||
for (size_t i = 0; i < 6; ++i) {
|
||||
body.legs[i].setPos(Leg::Hipp, double(o.attitude.roll) * 4);
|
||||
body.legs[i].setPos(Leg::Knee, double(o.attitude.pitch) * 4);
|
||||
@ -62,7 +61,7 @@ void sweepLeg() {
|
||||
}
|
||||
|
||||
void checkConfig() {
|
||||
Config::Data compData;
|
||||
FlashConfig::Data compData;
|
||||
strncpy(compData.wifiSSID.data(), wifiSSID, sizeof(compData.wifiSSID));
|
||||
strncpy(compData.wifiPass.data(), wifiPass, sizeof(compData.wifiPass));
|
||||
|
||||
@ -90,7 +89,6 @@ void setup() {
|
||||
connectWiFi();
|
||||
remote.reset(new OSCRemote(vbat));
|
||||
remote->registerCallback(testRemoteCallback);
|
||||
// remote->init();
|
||||
|
||||
sweepLeg();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user