MagneticSensorSPI and MA730 sensor

Assuming you are on SimpleFOC 1.6 and are happy to use default SPI (SPI1 default pins) then you should be able to use:

MagneticSensorSPI sensor = MagneticSensorSPI(MA730_SPI, CS_PIN);

Have you tried something like that?

My recent pull request allows you to use SPI2 or 3 (and non default pins for MOSI, MISO, SCLK). I suspect you don’t need that.

It would allow you to do something like:

SPIClass SPI_2(PB15, PB14, PB13);

void setup() {
  sensor.init(&SPI_2);

You can’t choose any pins, you’d have to follow datasheet for what is allowed for your board. The above is for my storm32 which uses an stm32f107 I think.

1 Like

ok I was trying
MagneticSensorSPI sensor = MagneticSensorSPI(15, 14, 0x0000);
But now realize I just needed to add

#define CS_PIN 15

so all all good now and working

1 Like

Spending a bit of time with the MA730 today getting some pretty big kickback error. I tried out setting the mode to 0 or 3 but not so lucky getting it so stable: Anyone have any tips?

What do you mean by kickback error. Do you mean that it’s position on startup changes violently then it starts to behave correctly? Or are you seeing some other problem? Can you describe in more detail.

video I guess helps explain things
https://streamable.com/vlq8xw

Also after some time it starts doing this on its own.
https://streamable.com/vvfypo

I seem to find all the good errors haha

You do seem to find the good errors. I’ve not seen this. It looks like the magnetic natural direction is randomly flipping! The closest thing I’ve seen is when I change zero_offset - this can cause direction to change.
I can see 7 cables going to your motor/sensor. I have 9.
3x motor phases and the other 6 (3.3v, gnd, cs, cclk, miso, mosi) as per page 14 of Test Board for MagAlpha Sensors User Guide

I’ve recently been told and agree that the magnetic sensors (i2c or spi) work better if you call them less regularly e.g. if you ensure motor.move(x) is called say at 400Hz rather than 'as fast as possible`. You do want to call loopFOC fast though. This gives a smoother velocity.

I have 3 phases, 3.3 gnd miso mosi clk and c’s (gpio15) and I also have a and b. Perhaps I need to find a way to fix that magnetic direction.

Also happening in encoder mode though perhaps not as bad. I can imagine that the error over time is heat and sensor drift.
https://streamable.com/k8jkvv

Checked all those pins again. Hell if I know what the issue is. It always seems to happen when the velocity gets over 20 in velocity mode and gradually gets worse when it gets faster. 4 hours of trying to to tune the pid loop so I am so with this sensor untill perhaps others try to run it and see what’s going on:p

I was going to suggest you use the AB signals to test. I’ve used them in the past up to around 100 rad/s with no issues. Have you a oscilloscope to see the signals? Can you also monitor the 3.3V line while operating?

@David_Gonzalez yes been trying the ab mode as well. Similar problems really
Made a video with the scope of you want me to check anything in particular I can do that:)
https://streamable.com/mnd2nu

Everything seems fine, I was worried something was wrong in the lines. What MCU are you using? do you experience the same behaviour in voltage mode?

Have you tried what Owen suggested? I’ve seen much better results in some of my projects calling motor.move() slower.

I am using an esp32 devkit c. Voltage mode also gets me into trouble. I got these boards from mouser so unless I am extremely unlucky they should be ok. I will test the second one and see how it goes.

Second board now trying also the bluepill. Just using A and B…still seems the same problemo. The board I got was
https://www.mouser.de/datasheet/2/277/TBMA850-Q-RD-01A-1624324.pdf
and its the board type 2. does say that cap2 is option and not populated(it is is on the borrd)
The board number is TBMA730-Q-RD-01A

Also tried updating motor.move less often without any effect

This is very weird, I had no problems at all with the MA730. Have you tried printing the encoder values? or maybe even try another code just to test the encoder. I have a quadrature encoder test code if you want to try it.

Would love to try something else if your willing to share the code. I must say it’s a bit of a ghost.

Let’s see if you can get proper position with the AB signals, this code is just that. Try spinning roughly half a revolution, full revolution, full negative revolution and check if the printed value seems correct.

//A and B encoder inputs
#define CANAL_A 32
#define CANAL_B 33
#define INDEX 13

//count refers to the number of encoder pulses that occured. You add to this number if the motor is rotating in one direction and substract if it's rotating the other direction
float count = 0, z = 0;
//This is the actual and previous state of the encoder magnet to determine direction of rotation
byte state, statep;
//Variables used in the handleInterrupt funcion to select the current state.
boolean A,B;

void handleInterrupt();
void indexPin();

void setup() {
  //Serial serial communication to computer
  Serial.begin(115200);

  pinMode(15,OUTPUT);
  digitalWrite(15,HIGH);

  //Pinmode for index input
  pinMode(INDEX, INPUT);
  attachInterrupt(digitalPinToInterrupt(33), indexPin, CHANGE);
  
  //Pinmodes for encoder inputs
  pinMode(CANAL_A, INPUT);
  pinMode(CANAL_B, INPUT);
  attachInterrupt(digitalPinToInterrupt(CANAL_A), handleInterrupt, CHANGE);
  attachInterrupt(digitalPinToInterrupt(CANAL_B), handleInterrupt, CHANGE);
}

void loop() {
  float angle = count *360/4096; //Your resolution here
  Serial.print("Times passed through index: ");
  Serial.print(z);
  Serial.print(", Current angle: ");
  Serial.println(angle);
}

void indexPin() {
  int index = digitalRead(33);
  if (index == 1) z += 1;
}

void handleInterrupt() {
    A = digitalRead(CANAL_A);
    B = digitalRead(CANAL_B);

    if ((A==HIGH)&&(B==HIGH)) state = 1;
    if ((A==HIGH)&&(B==LOW)) state = 2;
    if ((A==LOW)&&(B==LOW)) state = 3;
    if((A==LOW)&&(B==HIGH)) state = 4;

    switch (state)
    {
      case 1:
      {
        if (statep == 2) count++;
        if (statep == 4) count--;
        break;
      }
      case 2:
      {
        if (statep == 1) count--;
        if (statep == 3) count++;
        break;
      }
      case 3:
      {
        if (statep == 2) count --;
        if (statep == 4) count ++;
        break;
      }
      default:
      {
        if (statep == 1) count++;
        if (statep == 3) count--;
      }
    }
    statep = state;
}

Hi,
I’m just trying to get my TLE5012 encoder running on a stepper motor (still using AB). Its not running smooth yet, but not that problematic as yours.

  • have you checked if your magnet is perfectly centered on the shaft? (mine is not) - i’m suspecting that this can have effects on the encoder values as well ?!

Thanks David, the encoder seems to work fine. I doubt it’s the sensor itself now. It’s also not code seeing as nobody has my problem. I’m leaning on some feedback problem but slightly unsure now how to test that. New video with just testing the encoder.
https://streamable.com/w23n6p