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

1
.gitignore vendored
View File

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

View File

@ -1,11 +1,11 @@
#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;
@ -14,7 +14,7 @@ public:
Data& operator=(const Data&); Data& operator=(const Data&);
} Data; } Data;
Config(){}; FlashConfig(){};
void init(); void init();
@ -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

@ -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,7 +7,6 @@
#include <OSCMessage.h> #include <OSCMessage.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <list> #include <list>
class OSCRemote : public IRemote { class OSCRemote : public IRemote {

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,18 +1,18 @@
#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);
@ -20,7 +20,7 @@ void Config::load() {
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,14 +1,8 @@
#include "OSCRemote.h" #include "OSCRemote.h"
#include "config.h"
#include <array> #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 { namespace {
@ -29,7 +23,12 @@ 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;
@ -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
} }
} }

View File

@ -1,14 +1,12 @@
#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
@ -26,9 +24,8 @@
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,8 +54,7 @@ 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)
} }
@ -67,10 +63,8 @@ void checkForServoSleep() {
#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,13 +1,12 @@
#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 ---
@ -15,7 +14,7 @@ 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;
@ -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();
} }