Refactoring of config file

This commit is contained in:
Philip Johansson 2020-04-05 20:20:28 +02:00
parent 7ad64c2e51
commit afd4d20f23
12 changed files with 97 additions and 108 deletions

View File

@ -14,6 +14,7 @@ BraceWrapping:
BeforeCatch: true BeforeCatch: true
BeforeElse: true BeforeElse: true
PointerAlignment: Left PointerAlignment: Left
ColumnLimit: 100
AlwaysBreakAfterReturnType: None AlwaysBreakAfterReturnType: None
PenaltyReturnTypeOnItsOwnLine: 1000000 PenaltyReturnTypeOnItsOwnLine: 1000000

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
.vscode/c_cpp_properties.json .vscode/c_cpp_properties.json
.vscode/launch.json .vscode/launch.json
.vscode/ipch .vscode/ipch
include/settings.h include/settings.h
include/config.h

View File

@ -1,32 +1,32 @@
#include "config.h"
#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <array> #include <array>
#define EEPROM_SIZE 64 class FlashConfig {
class Config {
public: public:
typedef struct Data { typedef struct Data {
std::array<char, 16> wifiSSID; std::array<char, 16> wifiSSID;
std::array<char, 16> wifiPass; std::array<char, 16> wifiPass;
Data &operator=(const Data &); Data& operator=(const Data&);
} Data; } Data;
Config(){}; FlashConfig(){};
void init(); void init();
void load(); void load();
void save(); void save();
void save(Data &); void save(Data&);
void setSSID(String); void setSSID(String);
void setWifiPass(String); void setWifiPass(String);
const Data &data(); const Data& data();
void printConfig(); void printConfig();
@ -34,5 +34,5 @@ private:
Data _data; Data _data;
}; };
bool operator==(const Config::Data &a, const Config::Data &b); bool operator==(const FlashConfig::Data& a, const FlashConfig::Data& b);
bool operator!=(const Config::Data &a, const Config::Data &b); bool operator!=(const FlashConfig::Data& a, const FlashConfig::Data& b);

View File

@ -34,9 +34,9 @@ public:
bool isNew; bool isNew;
} Output; } Output;
virtual const Output &output() = 0; virtual const Output& output() = 0;
virtual void loop() = 0; virtual void loop() = 0;
virtual void registerCallback(void (*callback)(const Output &)) = 0; virtual void registerCallback(void (*callback)(const Output&)) = 0;
}; };

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "IRemote.h" #include "IRemote.h"
#include "Vbat.h" #include "Vbat.h"
#include <OSCBundle.h> #include <OSCBundle.h>
@ -8,19 +7,18 @@
#include <OSCMessage.h> #include <OSCMessage.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <list> #include <list>
class OSCRemote : public IRemote { class OSCRemote : public IRemote {
public: public:
//! Needs to be called in "Setup" //! Needs to be called in "Setup"
OSCRemote(const Vbat &); OSCRemote(const Vbat&);
void loop(); 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: private:
//! Place this after WiFi.begin() in main //! Place this after WiFi.begin() in main
@ -28,7 +26,7 @@ private:
IRemote::Output _output; IRemote::Output _output;
std::list<void (*)(const IRemote::Output &)> _callbacks; std::list<void (*)(const IRemote::Output&)> _callbacks;
const Vbat &_vbat; const Vbat& _vbat;
}; };

View File

@ -3,7 +3,6 @@
#include <map> #include <map>
#include <vector> #include <vector>
class Leg { class Leg {
public: public:
enum Joint { enum Joint {

19
include/config.h.orig Normal file
View 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)

View File

@ -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";

View File

@ -1,27 +1,27 @@
#include "config.h" #include "FlashConfig.h"
void Config::init() { void FlashConfig::init() {
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
} }
void Config::setSSID(String ssid) { void FlashConfig::setSSID(String ssid) {
strcpy(_data.wifiSSID.data(), ssid.c_str()); strcpy(_data.wifiSSID.data(), ssid.c_str());
}; };
void Config::setWifiPass(String pass) { void FlashConfig::setWifiPass(String pass) {
strcpy(_data.wifiPass.data(), pass.c_str()); strcpy(_data.wifiPass.data(), pass.c_str());
}; };
void Config::load() { void FlashConfig::load() {
byte *pBuff = (byte *)(const void *)&_data; byte* pBuff = (byte*)(const void*)&_data;
for (size_t i = 0; i < sizeof(_data); ++i) { for (size_t i = 0; i < sizeof(_data); ++i) {
*pBuff++ = EEPROM.read(i); *pBuff++ = EEPROM.read(i);
} }
Serial.println("EEPROM Loaded"); Serial.println("EEPROM Loaded");
}; };
void Config::save() { void FlashConfig::save() {
const byte *pBuff = (const byte *)(const void *)&_data; const byte* pBuff = (const byte*)(const void*)&_data;
for (size_t i = 0; i < sizeof(_data); ++i) { for (size_t i = 0; i < sizeof(_data); ++i) {
EEPROM.write(i, *pBuff++); EEPROM.write(i, *pBuff++);
} }
@ -29,29 +29,29 @@ void Config::save() {
Serial.println("EEPROM Saved"); Serial.println("EEPROM Saved");
}; };
void Config::save(Config::Data &data) { void FlashConfig::save(FlashConfig::Data& data) {
_data = data; _data = data;
save(); save();
} }
const Config::Data &Config::data() { const FlashConfig::Data& FlashConfig::data() {
return _data; return _data;
} }
void Config::printConfig() { void FlashConfig::printConfig() {
Serial.printf("wifiSSID: %s\n", _data.wifiSSID.data()); Serial.printf("wifiSSID: %s\n", _data.wifiSSID.data());
Serial.printf("wifiPass: %s\n", _data.wifiPass.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->wifiSSID = other.wifiSSID;
this->wifiPass = other.wifiPass; this->wifiPass = other.wifiPass;
return *this; 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); 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); return !(a == b);
} }

View File

@ -1,19 +1,13 @@
#include "OSCRemote.h" #include "OSCRemote.h"
#include "config.h"
#include <array> #include <array>
//! --- Start of onfiguration --- IRemote::Output* pOutput;
//#define DEBUG // Enables CM prints if commented
const unsigned int outPort = 9999;
const unsigned int inPort = 8888;
//! --- End of configuration ---
IRemote::Output *pOutput;
namespace { namespace {
WiFiUDP *pUdp; WiFiUDP* pUdp;
IPAddress remoteIP; IPAddress remoteIP;
// Scaling // Scaling
@ -26,10 +20,15 @@ const float vxFactor = 0.1;
const float vyFactor = 0.1; const float vyFactor = 0.1;
const float wFactor = 10; const float wFactor = 10;
void remoteMsgParser(OSCMessage &msg, int offset) { void remoteMsgParser(OSCMessage& msg, int offset) {
std::array<char, 20> topic; std::array<char, 20> topic;
msg.getAddress(topic.data()); 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")) { if (!strcmp(topic.data(), "/att/xy")) {
pOutput->attitude.pitch = msg.getFloat(0) * pitchFactor; 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); remoteMsgParser(msg, 0);
} }
@ -94,7 +93,7 @@ bool msgReceive() {
msg.fill(pUdp->read()); msg.fill(pUdp->read());
} }
#ifdef DEBUG #ifdef DEBUG_OSC
Serial.printf("OSC packet from: %s\n", remoteIP.toString().c_str()); Serial.printf("OSC packet from: %s\n", remoteIP.toString().c_str());
#endif #endif
@ -108,9 +107,8 @@ bool msgReceive() {
return true; return true;
} }
else { else {
#ifdef DEBUG #ifdef DEBUG_OSC
Serial.printf("error: %s\n", Serial.printf("error: %s\n", getOSCErrorName(msg.getError()).c_str());
getOSCErrorName(msg.getError()).c_str());
#endif #endif
} }
} }
@ -118,7 +116,7 @@ bool msgReceive() {
} }
} // namespace } // namespace
OSCRemote::OSCRemote(const Vbat &vbat) : _vbat(vbat) { OSCRemote::OSCRemote(const Vbat& vbat) : _vbat(vbat) {
init(); init();
} }
@ -151,10 +149,10 @@ void OSCRemote::loop() {
} }
} }
const IRemote::Output &OSCRemote::output() { const IRemote::Output& OSCRemote::output() {
return _output; return _output;
} }
void OSCRemote::registerCallback(void (*callback)(const IRemote::Output &)) { void OSCRemote::registerCallback(void (*callback)(const IRemote::Output&)) {
_callbacks.push_back(callback); _callbacks.push_back(callback);
} }

View File

@ -1,34 +1,31 @@
#include "body.h" #include "Body.h"
#include "config.h"
#include <Adafruit_PWMServoDriver.h> #include <Adafruit_PWMServoDriver.h>
#include <Arduino.h> #include <Arduino.h>
#include <SPI.h> #include <SPI.h>
#include <Wire.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 // 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 // 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 // for max range. You'll have to tweak them as necessary to match the servos you
// have! // have!
#define SERVOMIN 190 // This is the 'minimum' pulse length count (out of 4096) #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 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 600 // This is the rounded 'minimum' microsecond length based on the minimum
// pulse of 190 // pulse of 190
#define USMAX \ #define USMAX \
2400 // This is the rounded 'maximum' microsecond length based on the 2400 // This is the rounded 'maximum' microsecond length based on the
// maximum pulse of 540 // maximum pulse of 540
#define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates #define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates
Adafruit_PWMServoDriver servoDriver = Adafruit_PWMServoDriver(0x40); Adafruit_PWMServoDriver servoDriver = Adafruit_PWMServoDriver(0x40);
unsigned long freqWatchDog = 0; unsigned long freqWatchDog = 0;
unsigned long SuppressScamperUntil = unsigned long SuppressScamperUntil = 0; // if we had to wake up the servos, suppress the power
0; // if we had to wake up the servos, suppress the power hunger scamper // hunger scamper mode for a while
// mode for a while
void resetServoDriver() { void resetServoDriver() {
#ifndef CTRL_INACTIVE #ifndef CTRL_INACTIVE
@ -57,20 +54,17 @@ void checkForServoSleep() {
// beep(1200,100); // chirp to warn user of brown out on servo // beep(1200,100); // chirp to warn user of brown out on servo
// controller // controller
SuppressScamperUntil = SuppressScamperUntil =
millis() + millis() + 10000; // no scamper for you! (for 10 seconds because we ran out
10000; // no scamper for you! (for 10 seconds because we ran out // of power, give the battery a bit of time for charge
// of power, give the battery a bit of time for charge // migration and let the servos cool down)
// migration and let the servos cool down)
} }
freqWatchDog = millis() + 100; freqWatchDog = millis() + 100;
} }
#endif #endif
} }
double modifiedMap( double modifiedMap(double x, double in_min, double in_max, double out_min, double out_max) {
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 temp =
(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
temp = (int)(4 * temp + .5); temp = (int)(4 * temp + .5);
return (double)temp / 4; return (double)temp / 4;
} }
@ -90,10 +84,8 @@ void Body::healthCheck() {
} }
Leg::Leg(uint8_t hipp, uint8_t knee) Leg::Leg(uint8_t hipp, uint8_t knee)
: _joints( : _joints({{Joint::Hipp, {.index = hipp, .trim = 0, .center = 0.0, .pos = 0}},
{{Joint::Hipp, {.index = hipp, .trim = 0, .center = 0.0, .pos = 0}}, {Joint::Knee, {.index = knee, .trim = 0, .center = 45.0, .pos = 0}}}) {
{Joint::Knee,
{.index = knee, .trim = 0, .center = 45.0, .pos = 0}}}) {
} }
void Leg::setTrim(Joint j, uint8_t angle) { void Leg::setTrim(Joint j, uint8_t angle) {
@ -105,16 +97,11 @@ void Leg::setPos(Joint j, double angle) {
//! servo //! servo
#ifndef CTRL_INACTIVE #ifndef CTRL_INACTIVE
auto angleToPulse = [](double a) { // auto angleToPulse = [](double a) { return modifiedMap(a, -180.0, 180.0, SERVOMIN, SERVOMAX);
return modifiedMap(a, -180.0, 180.0, SERVOMIN, SERVOMAX); // } servoDriver.setPWM(_joints[j].index, 0, angleToPulse(angle));
};
// servoDriver.setPWM(_joints[j].index, 0, angleToPulse(angle));
auto angleToMicroPulse = [](double a) { auto angleToMicroPulse = [](double a) { return modifiedMap(a, -180.0, 180.0, USMIN, USMAX); };
return modifiedMap(a, -180.0, 180.0, USMIN, USMAX); servoDriver.writeMicroseconds(_joints[j].index, angleToMicroPulse(angle) + _joints[j].center);
};
servoDriver.writeMicroseconds(_joints[j].index,
angleToMicroPulse(angle) + _joints[j].center);
#endif #endif
_joints[j].pos = angle; _joints[j].pos = angle;

View File

@ -1,21 +1,20 @@
#include <Arduino.h> #include "FlashConfig.h"
#include <WiFi.h>
// Project includes
#include "OSCRemote.h" #include "OSCRemote.h"
#include "Vbat.h" #include "Vbat.h"
#include "body.h" #include "body.h"
#include "config.h"
#include "settings.h" #include "settings.h"
#include <Arduino.h>
#include <WiFi.h>
#include <memory> #include <memory>
//! --- Start of onfiguration --- //! --- Start of onfiguration ---
const char *hostname = "Hexapod"; const char* hostname = "Hexapod";
//! --- End of configuration --- //! --- End of configuration ---
Body body = Body::instance(); Body body = Body::instance();
Config config; FlashConfig config;
std::unique_ptr<IRemote> remote; std::unique_ptr<IRemote> remote;
Vbat vbat; Vbat vbat;
@ -40,7 +39,7 @@ void connectWiFi() {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
void testRemoteCallback(const IRemote::Output &o) { void testRemoteCallback(const IRemote::Output& o) {
for (size_t i = 0; i < 6; ++i) { for (size_t i = 0; i < 6; ++i) {
body.legs[i].setPos(Leg::Hipp, double(o.attitude.roll) * 4); body.legs[i].setPos(Leg::Hipp, double(o.attitude.roll) * 4);
body.legs[i].setPos(Leg::Knee, double(o.attitude.pitch) * 4); body.legs[i].setPos(Leg::Knee, double(o.attitude.pitch) * 4);
@ -62,7 +61,7 @@ void sweepLeg() {
} }
void checkConfig() { void checkConfig() {
Config::Data compData; FlashConfig::Data compData;
strncpy(compData.wifiSSID.data(), wifiSSID, sizeof(compData.wifiSSID)); strncpy(compData.wifiSSID.data(), wifiSSID, sizeof(compData.wifiSSID));
strncpy(compData.wifiPass.data(), wifiPass, sizeof(compData.wifiPass)); strncpy(compData.wifiPass.data(), wifiPass, sizeof(compData.wifiPass));
@ -90,7 +89,6 @@ void setup() {
connectWiFi(); connectWiFi();
remote.reset(new OSCRemote(vbat)); remote.reset(new OSCRemote(vbat));
remote->registerCallback(testRemoteCallback); remote->registerCallback(testRemoteCallback);
// remote->init();
sweepLeg(); sweepLeg();
} }