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
BeforeElse: true
PointerAlignment: Left
ColumnLimit: 100
AlwaysBreakAfterReturnType: None
PenaltyReturnTypeOnItsOwnLine: 1000000

1
.gitignore vendored
View File

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

View File

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

View File

@ -1,6 +1,5 @@
#pragma once
#include "IRemote.h"
#include "Vbat.h"
#include <OSCBundle.h>
@ -8,7 +7,6 @@
#include <OSCMessage.h>
#include <WiFiUdp.h>
#include <list>
class OSCRemote : public IRemote {

View File

@ -3,7 +3,6 @@
#include <map>
#include <vector>
class Leg {
public:
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);
}
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() {
void FlashConfig::load() {
byte* pBuff = (byte*)(const void*)&_data;
for (size_t i = 0; i < sizeof(_data); ++i) {
*pBuff++ = EEPROM.read(i);
@ -20,7 +20,7 @@ void Config::load() {
Serial.println("EEPROM Loaded");
};
void Config::save() {
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);
}

View File

@ -1,14 +1,8 @@
#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;
namespace {
@ -29,7 +23,12 @@ const float wFactor = 10;
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;
@ -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
}
}

View File

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

View File

@ -1,13 +1,12 @@
#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 ---
@ -15,7 +14,7 @@ const char *hostname = "Hexapod";
//! --- End of configuration ---
Body body = Body::instance();
Config config;
FlashConfig config;
std::unique_ptr<IRemote> remote;
Vbat vbat;
@ -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();
}