Sensor data over RS485

Hi there, i am actually experimenting with a 2 part haptic encoder.
I have one esp32 connected to my SPI Sensor reading the sensor using the simpleFOC library. Then I send the values over RS485 to the remote ESP32. But I don’t get how I give the transmitted values over to the “motor.linkSensor(&sensor);”.

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

#define RXD2 27 
#define TXD2 32

HardwareSerial rs485(1);

MagneticSensorSPIConfig_s MAQ430_SPI = {
  .spi_mode = SPI_MODE3,
  .clock_speed = 1000000,
  .bit_resolution = 12,
  .angle_register = 0x0000,
  .data_start_bit = 15,
  .command_rw_bit = 0,  // not required
  .command_parity_bit = 17 // parity not implemented
};

// MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs)
//  config  - SPI config
//  cs      - SPI chip select pin 
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(MAQ430_SPI, 5);
// alternative constructor (chipselsect, bit_resolution, angle_read_register, )
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);

// global variables
float old_angle;
float old_velocity;

void setup() {
  delay(2000);
  Serial.begin(115200);
  Serial.println("Serial is available");
  
  rs485.begin(115200, SERIAL_8N1, RXD2, TXD2);
  
  // initialise magnetic sensor hardware
  sensor.init();

  Serial.println("Sensor ready");
  Serial.println("Setup is done!");
  _delay(1000);
}

void loop() {
  // iterative function updating the sensor internal variables
  // it is usually called in motor.loopFOC()
  // this function reads the sensor hardware and 
  // has to be called before getAngle nad getVelocity
  sensor.update();

  // display the angle and the angular velocity to the terminal
  if (old_angle!=sensor.getAngle()){
      float current_angle = sensor.getAngle();
      float current_velocity = sensor.getVelocity();
      Serial.print(current_angle);
      Serial.print("\t");
      Serial.println(current_velocity);

      // send the angle over rs485
      rs485.print(current_angle);
      rs485.print('\n'); // Send newline character
      rs485.print(current_velocity);
      rs485.print('\n'); // Send newline character

      old_angle = current_angle;
  }
}


Thanks for any help!

For latency reasons I think it would make more sense to locally read the sensor and use that for motor control. If you need to know the position on the far end (not connected to motor) you could send that over whatever protocol you want, without hooking it into SFOC. I think the latency of encoding + decoding + cable travel could give you some trouble, especially for haptics.

If you already have an MCU for receiving data over POE/RS485, and a sensor, and motor driver, why try to control it from far away?

@VIPQualityPost My problem actually is that I have to accommodate everything in a pretty small and flat enclosure and my mainboard is not fitting inside the enclosure. The mainboard is ~1.5m away. so my workaround is to have my sensor and a small esp32 with RS485 transceiver in the enclosure under the motor and have the driver and mainboard esp32 with PoE in a second enclosure.

I’m fairly sure RS485 will be too slow, and the latency, as @VIPQualityPost has mentioned will be too high (reading the sensor, writing the serial, reading the serial - a small delay will be introduced each time).
If you get it to work at all, it will greatly limit your performance.

1.5m is quite a distance, it is too long for SPI without additional measures. But ABZ type signals may work. Also if you can find a sensor with differential ABZ (A,B,Z,A-,B-,Z-) it may work even better.

There are components you can get for transporting differential signals over significant distances 1.5m would be no problem. An ethernet type cable would be suitable. There are also ones that take in single-ended signals and output differential for transmission.

You could also build your own solution for example by converting the ABZ signals to 5V or 12V via a buffer and making a “loud” signal line to reliably bridge the 1.5m.

1 Like

Yes, differential signals is what I would use and what I know as working for industry automation servo drives, even with several meters cable length. Of course only if cable specs are correct (shielding, common signal ground.)