Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motor otrok #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions fw/rbcx-coprocessor/include/Motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Motor {
m_mode = MotorMode_POWER;
}

int16_t actualPower() const { return m_actualPower; }

bool atTargetPosition() const {
return uint32_t(abs(m_actualPosition - m_targetPosition))
<= m_posEpsilon;
Expand All @@ -41,14 +43,16 @@ class Motor {
MotorMode mode() const { return m_mode; }
void modeChange(MotorMode newMode) { m_mode = newMode; }

uint32_t coupleAxis() const { return m_coupleAxis; }

void reportStat(CoprocStat_MotorStat& stat) {
stat.mode = m_mode;
stat.position = m_actualPosition;
stat.power = m_actualPower;
stat.velocity = m_actualTicksPerLoop * motorLoopFreq;
};

int16_t poll(uint16_t encTicks) {
void poll(uint16_t encTicks) {
m_actualTicksPerLoop = encTicks - m_lastEncTicks;
m_actualPosition += m_actualTicksPerLoop;
m_lastEncTicks = encTicks;
Expand Down Expand Up @@ -93,7 +97,6 @@ class Motor {
default:
break;
}
return m_actualPower;
}

void setTargetPower(int16_t power) {
Expand Down Expand Up @@ -157,6 +160,13 @@ class Motor {
m_maxAccel = config.maxAccel / motorLoopFreq;
}

void setCoupling(const CoprocReq_SetMotorCoupling& coupling) {
if (m_mode != MotorMode_COUPLE_POWER) {
modeChange(MotorMode_COUPLE_POWER);
}
m_coupleAxis = coupling.coupleAxis;
}

private:
Regulator m_velocityReg;
Regulator m_positionReg;
Expand All @@ -171,4 +181,5 @@ class Motor {
uint16_t m_velEpsilon;
uint16_t m_maxAccel;
MotorMode m_mode;
int32_t m_coupleAxis;
};
2 changes: 1 addition & 1 deletion fw/rbcx-coprocessor/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ platform = ststm32@~7.0.0
board = genericSTM32F103VC
framework = stm32cube
lib_deps =
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/26267ccf1d3eda5c6aa973c3b0be9aaceb43c61f.zip
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/2a47451ed98dccde6b72d0e34c01cdda91a9dc88.zip

build_flags =
-Iinclude
Expand Down
23 changes: 21 additions & 2 deletions fw/rbcx-coprocessor/src/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ static void taskFunc() {
uint16_t encTicks = LL_TIM_GetCounter(encoderTimer[m]);
auto& targetMotor = motor[m];
auto modeBefore = targetMotor.mode();
auto action = targetMotor.poll(encTicks);
targetMotor.poll(encTicks);
auto modeAfter = targetMotor.mode();
setMotorPower(m, action, modeAfter == MotorMode_BRAKE);

if (modeBefore == MotorMode_POSITION
&& modeAfter == MotorMode_POSITION_IDLE) {
Expand All @@ -104,6 +103,18 @@ static void taskFunc() {
dispatcherEnqueueStatus(stat);
}
}
// Loop split due to possible dependency when coupling one motor's output to another:
for (int m : { 0, 1, 2, 3 }) {
auto& currentMotor = motor[m];
auto& targetMotor
= currentMotor.mode() != MotorMode_COUPLE_POWER
? currentMotor
: motor[currentMotor.coupleAxis()];

auto power = targetMotor.actualPower();
bool brake = targetMotor.mode() == MotorMode_BRAKE;
setMotorPower(m, power, brake);
}
}
vTaskDelayUntil(&now, pdMS_TO_TICKS(1000 / motorLoopFreq));
}
Expand Down Expand Up @@ -160,6 +171,14 @@ void motorDispatch(const CoprocReq_MotorReq& request) {
case CoprocReq_MotorReq_setConfig_tag:
targetMotor.setConfig(request.motorCmd.setConfig);
break;
case CoprocReq_MotorReq_setCoupling_tag:
if (request.motorCmd.setCoupling.coupleAxis > 3) {
DEBUG("Motor coupling axis %u invalid",
request.motorCmd.setCoupling.coupleAxis);
return;
}
targetMotor.setCoupling(request.motorCmd.setCoupling);
break;
}
}

Expand Down