Support for TLE5012B (magnetic angle sensor)

Hi!, i’ve been working with the TLE5012B sensor and library from the manufacture for arduino (TLE5012B - Arduino Reference).

I just follow the doc about supporting new sensor, i create the . h file and cpp file but when i initialize the motor.init(), it crashes and stop working or just restart.

I guess there is an compatibility issue between the TLE5012B library and FOC lib.

Here is the h file:

#include "Arduino.h"
#include <SimpleFOC.h>


class MySensor: public Sensor{
 public:
    MySensor();

    // initialize the sensor hardware
    void init();

    // Get current shaft angle from the sensor hardware, and 
    // return it as a float in radians, in the range 0 to 2PI.
    //  - This method is pure virtual and must be implemented in subclasses.
    //    Calling this method directly does not update the base-class internal fields.
    //    Use update() when calling from outside code.
    float getSensorAngle();


};

and here the cpp file:

#ifndef MySensor_h
#define MySensor_h

#include <TLE5012-ino.hpp>
#include "MySensor.h"

Tle5012Ino Tle5012Sensor = Tle5012Ino();
errorTypes checkError = NO_ERROR;

extern double d;

MySensor::MySensor(){
   
}
void MySensor::init(){
   checkError = Tle5012Sensor.begin();
}

float MySensor::getSensorAngle(){
    
    Tle5012Sensor.getAngleValue(d);
    d = (d + 180);
    if ( d + 40.9 > 360 ) {
        d = (d + 40.9) - 360;
    }
    else
        d = d + 40.9;

    d = d * 0.0174533;
    return d;
}


#endif

THKS!

Dear @EESOROPEZA ,

I must admit I am not sure what is the problem from your description.

The TLE5012 is not directly supported by SimpleFOC, but it looks like you are taking the right kind of steps to get it supported.

I have some TLE5012 boards here waiting to be tested, I will try to get to this over Christmas and create an “official” driver for them if I can get them working.

In the meantime, if you could monitor the SSI communication using a logic analyser or digital oscilloscope, you might find more information to help you debug the problem.

Regards,

Richard

Great @runger !!, i’ve tested this sensor with the stand alone library from infineon and works great, that’s the reason i want to use this sensor with your nice library, i guess the problem is with some clocks/timers initialization within both libraries… maybe at the time FOC configure the timers /clocks the TLE library just cant comunnicate with the sensor may be too fast or too slow…

I will analyze with the osciloscope SPI lines and post the results using FOC and not using it.

Best Regards and happy hollydays!

Hi @runger wondering if you had the chance to check it out?

Best Regards

Hey @EESOROPEZA ,

Maybe you can try the generic sensor implementation:

You’ll need to use the dev branch though. :smiley:

Your arduino file would look something like this:



#include <SimpleFOC.h>
#include <TLE5012-ino.hpp>

Tle5012Ino Tle5012Sensor = Tle5012Ino();
errorTypes checkError = NO_ERROR;

float readMySensor(){
 // read my sensor
 // return the angle value in radians in between 0 and 2PI
 
    Tle5012Sensor.getAngleValue(d);
    d = (d + 180);
    if ( d + 40.9 > 360 ) {
        d = (d + 40.9) - 360;
    }
    else
        d = d + 40.9;

    d = d * 0.0174533;
    return d;
}
void initMySensor(){
  // do the init
   checkError = Tle5012Sensor.begin();
}

// empty constructor 
GenericSensor sensor = GenericSensor(readMySensor, initMySensor);

void setup(){
...
  //init sensor and use it further with foc...
  sensor.init();
....
}

Hey, actually I have it in front of me right now, I was working on it the last few days.

I have based my solution on this code here:

which was written by another member of this forum.

I’m trying to make it more general, i.e. not use hard-coded pins and SPI instance.

However, this code I’m extending is specific to STM32 MCUs. The library you’re using is for AVR and InfineonXMC…

The problem is that this sensor uses half duplex SPI, an uncommon thing that is not implemented in Arduino framework. So you have to speak to the sensor “outside of” Arduino framework. The Infineon library certainly looks like a comprehensive implementation… they provide a cross-platform SPI3w abstraction to fit Arduino’s concept. :-/ perhaps a bit overkill just for reading the sensor, and its hard to see what’s going on exactly. It looks like they might be using timers.

Which MCU are you using? If you’re on one of the supported architectures, I think Antun’s suggestion is a very good one.
If you’re on STM32, I might have a solution for you in a few days, but no guarantees, its not working for me yet :slight_smile:

So, for STM32, I have it working. Its just an initial implementation, you can currently find it here:

For the moment, you could copy the classes to your project if you want to try it out. In the next weeks we’ll probably manage a proper release, but for now it is on this branch in git.

Here’s what half-duplex SPI looks like:

And this is my test setup - haven’t tried it with a motor yet, btw, just checked it outputs 0-2PI as you turn the motor by hand…

I’ll try to give it some more time in the next weeks, I’d like to get the velocity register working to compare that with the computed velocity we normally use…

1 Like