> For the complete documentation index, see [llms.txt](https://tk233.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tk233.gitbook.io/notes/motor-control/recoil-foc-motor-controller/0x03.-application.md).

# 0x03. Application

## SRAM Stack

### Motor Controller

**Encoder** encoder

The struct that holds the Encoder object.

**PowerStage** powerstage

The struct that holds the PowerStage object.

**Motor** motor

The struct that holds the Motor object.

**CurrentController** current\_controller

The struct that holds the CurrentController object.

**PositionController** position\_controller

The struct that holds the PositionController object.

**Mode** mode

Mode of the motor controller. See Mode of Operation.

**ErrorCode** error

Error code, if any.

**uint8\_t** device\_id

CAN ID of this device. The possible range is 1-63

**uint32\_t** firmware\_version

The version of the firmware. The 32 bit number is segmented as MMMM.mm.pp, where M is major version, m is minor version, and p is patch version.

### Encoder

**I2C\_HandleTypeDef** \*hi2c

This is the pointer to the i2c handler that connects to the I2C encoder.

**uint8\_t** i2c\_buffer\[2]

This buffer stores the raw byte data read from the encoder

**uint8\_t** i2c\_update\_counter

**int32\_t** cpr

**float** position\_offset

in range (-inf, inf)

**float** filter\_alpha

**float** flux\_offset

**float** flux\_offset\_table\[128]

**int16\_t** position\_raw

in range \[-cpr/2, cpr/2)

**int32\_t** n\_rotations

**float** position

in range (-inf, inf), with offset

**float** velocity

## Mode of operation

| Mode                 | Mode Id |
| -------------------- | ------- |
| DISABLED             | 0x00    |
| IDLE                 | 0x01    |
| DAMPING              | 0x02    |
| CALIBRATION          | 0x05    |
| CURRENT              | 0x10    |
| TORQUE               | 0x11    |
| VELOCITY             | 0x12    |
| POSITION             | 0x13    |
| VABC\_OVERRIDE       | 0x20    |
| VALPHABETA\_OVERRIDE | 0x21    |
| VQD\_OVERRIDE        | 0x22    |
| DEBUG                | 0x80    |

#### DISABLED Mode

In this mode, the gate driver is not activated. The motor is disabled with three phase wires floating. The motor is free to rotate.

Any error in other mode will trigger the state machine to enter DISABLED mode. The error code can be seen in the error\_code register.

Switching from DISABLED mode to any other mode than IDLE is prohibited and will result in an error state.

#### IDLE Mode

The gate driver is activated and outputs zero or balanced output. Motor is in free spinning mode.

Upon entering this mode, all the error status is cleared.

#### DAMPING Mode

The gate driver is activated and outputs zero. Motor is in damping mode.

#### CALIBRATION Mode

The motor controller will execute the calibration sequence in the main loop.

After calibration, the motor controller will set itself in IDLE mode.

#### CURRENT Mode

Current closed-loop mode.

User sets `i_q_target` and `i_d_target` to control the motor.

#### TORQUE Mode

Torque closed-loop mode.

User sets `torque_target` to control the motor.

#### VELOCITY Mode

Velocity closed-loop mode.

User sets `velocity_target` to control the motor.

#### POSITION Mode

Position closed-loop mode.

User sets `position_target` to control the motor.

#### VABC\_OVERRIDE Mode

Open loop controlled V\_a, V\_b, V\_c.

User sets `v_a_setpoint`, `v_b_setpoint`, and `v_c_setpoint` to control the motor.

#### VALPHABETA\_OVERRIDE Mode

Open loop controlled V\_alpha and V\_beta.

User sets `v_alpha_setpoint` and `v_beta_setpoint` to control the motor.

#### VQD\_OVERRIDE Mode

Open loop controlled V\_d and V\_q.

User sets `v_d_target` and `v_q_target` to control the motor.

#### DEBUG Mode

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8d6180ff-d522-4a3a-b959-851f14256471/Untitled.png)

## CAN Communication

Recoil motor controller implements a subset of the CANopen communication protocol.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tk233.gitbook.io/notes/motor-control/recoil-foc-motor-controller/0x03.-application.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
