Skip to content

Commit

Permalink
Merge pull request #20 from adamb314/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adamb314 authored Mar 19, 2024
2 parents 0eb33ca + a1b8ac7 commit 68a5dba
Show file tree
Hide file tree
Showing 180 changed files with 60,983 additions and 6,898 deletions.
45 changes: 24 additions & 21 deletions ArduinoLibrary/ServoProjectController/ServoProjectController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ void DCServoCommunicator::setControlSpeed(unsigned char controlSpeed,
this->controlSpeed = controlSpeed;
this->velControlSpeed = velControlSpeed;
this->filterSpeed = filterSpeed;
inertiaMarg = stdmin::min(stdmin::max(inertiaMarg, 1.0), 1.0 + 255.0 / 128);
this->inertiaMarg = static_cast<unsigned char>(stdmin::round((inertiaMarg - 1.0) * 128.0));
inertiaMarg = stdmin::min(stdmin::max(inertiaMarg, 1.0f), 1.0f + 255.0f / 128);
this->inertiaMarg = static_cast<unsigned char>(stdmin::round((inertiaMarg - 1.0f) * 128.0f));
}

void DCServoCommunicator::setBacklashControlSpeed(unsigned char backlashCompensationSpeed,
Expand Down Expand Up @@ -273,29 +273,18 @@ void DCServoCommunicator::setReference(const float& pos, const float& vel, const
{
newPositionReference = true;
newOpenLoopControlSignal = false;
refPos = (pos - offset) / scale * positionUpscaling;
refPos = stdmin::round((pos - offset) / scale * positionUpscaling);
refVel = stdmin::round(vel / scale * velocityUpscaling);

int sign = 0;
if (vel > 0.0f)
{
sign = 1;
}
else if (vel < 0.0f)
{
sign = -1;
}
refVel = stdmin::round(vel / scale);
refVel = stdmin::max(1, stdmin::abs(refVel)) * sign;

if (refVel > 4)
if (refVel > 4 * velocityUpscaling)
{
frictionCompensation = stdmin::abs(frictionCompensation);
}
else if (refVel < -4)
else if (refVel < -4 * velocityUpscaling)
{
frictionCompensation = -stdmin::abs(frictionCompensation);
}
this->feedforwardU = feedforwardU + frictionCompensation;
this->feedforwardU = stdmin::round(feedforwardU + frictionCompensation);
}

void DCServoCommunicator::setOpenLoopControlSignal(const float& feedforwardU, bool pwmMode)
Expand Down Expand Up @@ -405,12 +394,18 @@ DCServoCommunicator::OpticalEncoderChannelData DCServoCommunicator::getOpticalEn
return opticalEncoderChannelData;
}

float DCServoCommunicator::getScaling()
float DCServoCommunicator::getLowLevelControlError() const
{
activeCharReads[12] = true;
return scale * lowLevelControlError;
}

float DCServoCommunicator::getScaling() const
{
return scale;
}

float DCServoCommunicator::getOffset()
float DCServoCommunicator::getOffset() const
{
return offset;
}
Expand Down Expand Up @@ -539,7 +534,7 @@ CommunicationError DCServoCommunicator::run()
encoderPos = intReadBufferIndex10Upscaling.get() * (1.0f / positionUpscaling);
backlashCompensation = intReadBufferIndex11Upscaling.get() * (1.0f / positionUpscaling);

encoderVel = intReadBuffer[4];
encoderVel = intReadBuffer[4] * (1.0f / velocityUpscaling);
controlSignal = intReadBuffer[5];
current = intReadBuffer[6];
pwmControlSignal = intReadBuffer[7];
Expand All @@ -550,6 +545,8 @@ CommunicationError DCServoCommunicator::run()
opticalEncoderChannelData.minCostIndex = intReadBuffer[14];
opticalEncoderChannelData.minCost = intReadBuffer[15];

lowLevelControlError = static_cast<signed char>(charReadBuffer[12]) * 0.001f;

if (!isInitComplete())
{
++initState;
Expand All @@ -573,6 +570,12 @@ CommunicationError DCServoCommunicator::run()
if (isInitComplete())
{
updateOffset();

breakingChangeNr = static_cast<unsigned char>(charReadBuffer[15]);
if (breakingChangeNr >= 1)
{
velocityUpscaling = 8;
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions ArduinoLibrary/ServoProjectController/ServoProjectController.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ class DCServoCommunicator

OpticalEncoderChannelData getOpticalEncoderChannelData() const;

float getScaling();
float getLowLevelControlError() const;

float getOffset();
float getScaling() const;

float getOffset() const;

CommunicationError run();

Expand Down Expand Up @@ -355,6 +357,8 @@ class DCServoCommunicator
short int loopTime{0};
OpticalEncoderChannelData opticalEncoderChannelData;

float lowLevelControlError{0.0};

long int refPos{0};
CppArray<long int, 5> activeRefPos{0};
short int refVel{0};
Expand All @@ -367,6 +371,14 @@ class DCServoCommunicator
float scale{1.0f};

static constexpr int positionUpscaling = 32;
int velocityUpscaling = 1;

// ----------------------------------------
// ---- Communication breaking changes ----
// ----------------------------------------
// 0 : version <= 4.0
// 1 : version >= 4.1 : breaking change is velocityUpscaling = 8 > 1
unsigned char breakingChangeNr{0};
};

template <size_t N>
Expand Down
2 changes: 2 additions & 0 deletions ArduinoSketch/ArduinoSketch.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "src/ArduinoC++BugFixes.h"
#include "src/Hardware/ThreadHandler.h"
#include "src/Hardware/FailSafeHandler.h"

#include "config/config.h"

Expand All @@ -18,5 +19,6 @@ void setup()

void loop()
{
FailSafeHandler::getInstance()->resetWatchdogTimer();
communication->run();
}
2 changes: 1 addition & 1 deletion ArduinoSketch/config/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SetupConfigHolder : public DefaultConfigHolder

static std::unique_ptr<EncoderHandlerInterface> createOutputEncoderHandler()
{
constexpr static std::array<int16_t, 513> compVec = {0};
constexpr static std::array<int16_t, 1025> compVec = {0};
return std::make_unique<ResistiveEncoderHandler>(A1, 4096.0f * 180.0f / 360.0f, compVec);
}

Expand Down
127 changes: 127 additions & 0 deletions ArduinoSketch/config/defaultDS3225.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#ifndef CONFIG_HOLDER_H
#define CONFIG_HOLDER_H

#include "../defaultConfigHolder.h"

class SetupConfigHolder : public DefaultConfigHolder
{
public:
static std::unique_ptr<OpticalEncoderHandler> createMainEncoderHandler()
{
constexpr static std::array<uint16_t, 2048> aVec = {0};
constexpr static std::array<uint16_t, 2048> bVec = {0};

return std::make_unique<OpticalEncoderHandler>(aVec, bVec, A3, A4, 4096.0f * 10.0f / 1 * 11.0f / 62 * 14.0f / 48 * 13.0f / 45 * 1.0f / 42);
}

static std::unique_ptr<EncoderHandlerInterface> createOutputEncoderHandler()
{
constexpr static std::array<int16_t, 1025> compVec = {0};
return std::make_unique<ResistiveEncoderHandler>(A1, 4096.0f * 220.0f / 360, compVec);
}

static std::unique_ptr<CurrentController> createCurrentController()
{
constexpr float pwmToStallCurrent{1.0f};
constexpr float backEmfCurrent{0.0f};

auto pwmHighFrqCompFun = [](uint16_t in)
{
return in;
};
auto pwm = std::make_unique<HBridgeHighResPin11And12Pwm>(true, true, pwmHighFrqCompFun, 24000);
return std::make_unique<CurrentControlModel>(pwmToStallCurrent, backEmfCurrent, std::move(pwm));
}

class DefaultControlParametersWithPositionCompensation : public SetupConfigHolder::DefaultControlParameters
{
public:
static std::array<int16_t, 512> getPosDepForceCompVec()
{
constexpr static std::array<int16_t, 512> vec = {0};
return vec;
}

static std::array<int16_t, 512> getPosDepFrictionCompVec()
{
constexpr static std::array<int16_t, 512> vec = {0};
return vec;
}
};

class ControlParameters : public DefaultControlParametersWithPositionCompensation
{
public:
//kalman filter observer vector
static Eigen::Matrix<float, 3, 7> getKVector()
{
Eigen::Matrix<float, 3, 7> K;
K << -1.9619302890043613e-23f, 6.763936428834821e-19f, -1.0211439280426281e-14f, 8.966380809069127e-11f, -5.026553398907519e-07f, 0.0017546745669688208f, -0.01965617492779689f,
2.5494778082386736e-19f, -7.113271986018928e-15f, 7.773198181849748e-11f, -4.0726853722124173e-07f, 0.0008984664748649702f, 0.3759747733999441f, -61.62201356939168f,
5.093177792677117e-19f, -1.8966263561104195e-14f, 2.783998596483012e-10f, -1.9943485906824697e-06f, 0.006503382629253611f, -2.7471624009629547f, 339.77668409510306f;

return K;
}

//system model A matrix
static Eigen::Matrix3f getAMatrix()
{
Eigen::Matrix3f A;
A << 1.0f, 0.0005936462787358029f, 1.887871435352648e-05f,
0.0f, 0.9788959531583871f, 0.06270613007078364f,
0.0f, 0.0f, 1.0f;

return A;
}

//system model invers A matrix
static Eigen::Matrix3f getAInvMatrix()
{
Eigen::Matrix3f AInv;
AInv << 1.0f, -0.0006064447164383669f, 1.9149086916197254e-05f,
0.0f, 1.021559029612413f, -0.0640580133858595f,
0.0f, 0.0f, 1.0f;

return AInv;
}

//system model B matrix
static Eigen::Vector3f getBVector()
{
Eigen::Vector3f B;
B << 1.887871435352648e-05f,
0.06270613007078364f,
0.0f;

return B;
}

static bool internalFeedForwardEnabled()
{
return true;
}

//system model friction comp value
static float getFrictionComp()
{
return 58.36825891129791f;
}
};
};

class ConfigHolder
{
public:
static std::unique_ptr<Communication> getCommunicationHandler()
{
Serial.begin(115200);
Serial1.begin(115200);
auto com = std::make_unique<Communication>(SerialComOptimizer(&Serial1, &Serial));
com->addCommunicationNode(
std::make_unique<DCServoCommunicationHandler>(1, createDCServo<SetupConfigHolder>()));

return com;
}
};

#endif
Loading

0 comments on commit 68a5dba

Please sign in to comment.