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!