VESC FOC Encoder position control

Rotary Position Sensor + VESC

As it turns out you do not really need to make modifications to remove the hall sensor filter in order to get Rotary Position Sensors to work with the VESC. In a previous post I described how I located the hall sensor filter and removed it (read here).

Anyways, I was looking around the internet and found this blog which describes how to do it.

there is actually another port on the board you can use that is faster (hardware SPI instead of software SPI) and does not require previous mods mention here – just a change in a header file for the firmware.

By uncommenting this line in conf_general.h will enable the Hardware pins 

#define AS5047_USE_HW_SPI_PINS 

When changes have been made you need to compile the firmware and upload it to the VESC.
The Hardware pins is located here:

hardware SPI VESC encoder
hardware SPI VESC encoder

It has the following pin layout  (JSTPH 2mm 7pin)

5V - 3V - GND - Clk - CSn -MOSI -MISO

To connect the Rotary Position Sensor simply connect the corresponding ports to the VESC. I use the 3V and leave the 5V empty. It also highly recommend to use a shielded cable or try to keep the wires below 7 cm to reduce electric noise.

In my earlier post I connected the AS5048A over Software SPI on the hall sensor ports and had problems reading 0-180 degrees rotor position, but 180-360 degrees worked perfect. I posted a topic on the VESC forum an got a suggestion to ignore the MSB and use 13 bits instead of 14 with the following code in encoder.c

void encoder_tim_isr(void) { 
uint16_t pos; 

spi_transfer(&pos, 0, 1); 

pos &= 0x1FFF; 
last_enc_angle = ((float)pos * 360.0) / 8192.0; }

It worked perfect, I could now read 0-360 degrees rotor position with the AS5048A. The Rotary Position Sensor was running 13-bits, 8192 positions per revolution. So not optimal for a Rotary Position Sensor with 16384 positions per revolution. Please note that this fixed the problem with Software SPI over hall sensor port. 

The guy that I wrote about earlier in the post also had an alternative BLDC stack for his rover, which included a modified encoder.c with changes to keep track of the continuous encoder position. Connecting the VESC to the hardware SPI and uploaded his modified stack to the my VESC solved all problems, I was now running hardware SPI with 14-bits (16384 positions per revolution).

The AS5048A does not allow ABI interface, the AS5047P and the AS5047D does. I previously connected the AS5047D over ABI @2000 positions per revolution sucessfully. A disadvantage of the ABI mode is that it only gives a valid angle after detecting the index pulse. In SPI mode a valid angle is provided from the start.

Here is a small test of the AS5048A Rotary Position Sensor with the temporary drive system. Motor brackets are still 3D printed and not perfect (waiting for CNC Milling), therefore some annoying noises! Running VESC FOC Mode @10% Duty.




Chief Technology Officer with a demonstrated history of working in the internet industry. Skilled in Business Planning, Computer-Aided Design (CAD), Quality Process Development, and SolidWorks. Strong business development professional with a Higher Education Diploma with specialization in Mechanical Engineering focused in Product development from Blekinge Institute of Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *