I am working on servo motors control through serial communication protocol, I am sending data from Simulink to arduino and vice versa, The problem I am getting is that once I open Simulink Servo motors start turning while no command is sent or received from both, I tought that Simulink is initializing (reseting) arduino, so I decided to add a capacitor between Gnd & reset (I also tried with a resistor between 5v & reset), It actually works when I try to use Arduino IDE, Arduino is not reseting, but When i use it with Simulink it doesn't make change, Servo motors still turning randomly (I think that when I start or close simulation, Servo Are going to their initial position, I am not sure). I tried to add initial Position in Setup() function but in vain, It doesn't make sens! I tried using different arduino cards (MEGA, ESP32...Etc.) but in vain.
Does anyone have an idea about how to figure out this problem? Here is Simulink control code : & Arduino code :
#include <SCServo.h>
SCSCL sc;
//Estructura Union
typedef union {
float number;
uint8_t bytes[4];
} valor;
valor velocidad;
unsigned long timeold;
int vel = 0;
float value;
void setup() {
Serial1.begin(1000000);
sc.pSerial = &Serial1;
Serial.begin(9600);
}
void loop() {
//Pregunta si tenemos un dato a recibir
if (Serial.available() > 0) {
value = recepcion();
}
//Transforma el valor del voltaje (0-5) a velocidad
vel = map(value, 0, 5, 0, 1023);
//Activa el motor dirección Backward con la velocidad
sc.WritePos(3, value, 0);
velocidad.number = sc.ReadPos(3);
Serial.write('V');
for (int i = 0; i < 4; i++) {
Serial.write(velocidad.bytes[i]);
}
Serial.write('\n');
delay(50);
}
//Recibir Flotante
float recepcion() {
int i;
valor buf;
for (i = 0; i < 4; i++)
buf.bytes[i] = Serial.read();
return buf.number;
}
//Función para la lectura del encoder
Thank you !
