Custom Encoder Class?

Hi all,

I’m a new SimpleFOC user and currently working on a project to make a mini robotic arm that would have two encoders:

  1. Input: Incremental Optical - Quadrature
  2. Output: Absolute Magnetic - SPI

The goal is to use the output encoder to get better resolution. The input side is what SimpleFOC would use in the feedback loop to control the position. I already have standalone code for the sensors to work without SimpleFOC. I’m wondering if there is a way to import this code to use instead of the built in Quadrature and SPI classes and how I should go about correctly linking the sensor. Is this something that can be simple to implement? At least for the magnetic encoder, reading and writing the registers is different from the default settings in SimpleFOC and I’d feel more comfortable using my existing code. Does anyone have any thoughts on where to go with this?

Hey @Isa.gmz,

The simplest way would be to use the generic sensor:

You would just need to implement the function that returns the motor’s angle between 0-2pi

float readMySensorCallback(){
 // read my sensor
 // return the angle value in radians in between 0 and 2PI
 return ...;
}

And then provide it to the constructor

// GenericSensor class constructor
//  - readCallback pointer to the function reading the sensor angle
//  - initCallback pointer to the function initialising the sensor (optional)
GenericSensor sensor = GenericSensor(readMySensorCallback);

In the setup function then you can just link it to the motor as usual.

motor.linkSensor(&sensor);

The other way to go would be to implement your own sensor class, extending the Sensor interface. The idea is very similar but in a bit more clean form.

Here are some docs: