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

AMCFOC: Refactor motor-specific supervisor to get around codegen issues #68

Merged
merged 16 commits into from
Nov 27, 2023

Conversation

mfussi66
Copy link
Member

@mfussi66 mfussi66 commented Nov 27, 2023

This PR addresses #65 and targets the AMCFOC and Common board projects, with a refactor of the motor supervisor.

Objective

Fixing the codegen errors that occur when enclosing the motion controller in a ForEach subsystem, which is useful for driving multiple motors independently.

The problem

The C code generation did not work for the motor-specific supervisor because the function-call triggered subsystems had the trigger inputs of size >1. They are highlighted in the picture below.

283083264-f36aa965-bd55-4d4e-97cc-3afb2675421a

Not only it didn't work, it was not even possible to simulate the model.

The solution

Since the logic of the system is essentially a big parallel FSM (finite state machine), a refactor was made to wrap all the blocks in a single parallel Stateflow system, included in the ms2.slx file:

immagine

Each single fsm is included, in the same form as before, or functionally equivalent:

immagine

The charts run in parallel, aka they all run at the same tick, in the following order:

  1. First, fault manager
  2. Inputs dispatcher
  3. Control mode handler
  4. Last, Targets manager

They use local data to exchange information, and local events to trigger transitions between each other:

immagine

While it's not easy to find at a glance where data is read/written, the highlighting capabilities of the Simulink GUI can solve all doubts.

Fault manager

Works exactly as before: two parallel charts, one for handling the fault button, and the other for the hardware faults, aka overcurrent check at the moment.

immagine

Inputs dispatcher

The inputs dispatcher receives a mega data structure containing all possible types of inputs, and depending on the type of event, it triggers the execution of the related subcharts:

immagine

The charts on the inside are stripped-down versions of the ones before: they run only when they are called, then return. See for example the limits handler:

immagine

An exception to this pattern is the control mode set, which just triggers an event read by the Control mode handler.

Control mode handler

This is almost a copy-paste of the one already implemented, but with some subtle changes:

  • when a change of ctrl mode causes a setpoint change, the related event directly addresses the Targets Manager chart. This is a required pattern when working with local events in parallel charts
  • There are no more functions called isCtrlModePos(), isCltrModeCurrent() etc : now there is just isReqMode(mode)

immagine

Warning: The custom C functions (rtw_enable/disable_motors) were disabled for the purpose, they will be added back asap.

Targets manager

The targets manager is a new addition to the charts, and it reuses part of the SetPointHandler logic to handle targets both from the control mode handler and outside:

immagine

Note that we can use the function in(ControlModeHandler.<mode>) to check which state the ControlMode Handler is in (and therefore the mode of the board) and write the proper setpoint.

We also implement a graphical function to experiment with it, which probably is more efficient than an interpreted matlab function.

Tests

Proper harnesses will come in the future, but for now there is a test model called test_supervisorV2.slx that confirms the correct operation:

MATLAB_Bepv9RBCB2.mp4

Code generation

The code generation now works successfully, both in C, C++11, and inside the for each.

Note that this is not supposed to be a deus ex machina system, and can be changed in the future. Though now it's a bit simplified.

@mfussi66 mfussi66 self-assigned this Nov 27, 2023
@mfussi66 mfussi66 requested a review from pattacini as a code owner November 27, 2023 11:49
@pattacini pattacini changed the base branch from master to devel November 27, 2023 11:53
Copy link
Member

@pattacini pattacini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesomely done!
I've only changed the base branch to devel, which was equal to master by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AMCFOC: refactor motor-specific supervisor to be used with For Each subsystem
2 participants