Hi Paul,
Thank you for the advice, I will add tags in the next posts.
Actually here is my code :
// Create a union to easily convert float to byte
#include <SCServo.h>
#include <Pixy2.h>
int x = 0; //positon x axis
int y = 0; //position y axis
// This is the main Pixy object
Pixy2 pixy;
SCSCL sc;
#define RXD2 16
#define TXD2 17
//Read Position Variables
int16_t ReadP3 = 0;
int16_t ReadP2 = 0;
int S1,S2,S3; //Save Servo Angle
typedef union{
float number;
uint8_t bytes[4];
} FLOATUNION_t;
// Create the variable you want to send (Position)
FLOATUNION_t myValue1;
FLOATUNION_t myValue2;
FLOATUNION_t myValue3;
FLOATUNION_t send1;
FLOATUNION_t send2;
FLOATUNION_t send3;
FLOATUNION_t send4;
FLOATUNION_t send5;
void setup() {
// initialize serial, use the same boudrate in the Simulink Config block
Serial.begin(500000);
Serial2.begin(1000000, SERIAL_8N1, RXD2, TXD2);
sc.pSerial = &Serial2;
sc.WritePos(1, 300, 0);
sc.WritePos(2, 300, 0);
sc.WritePos(3, 300, 0);
pixy.init();
pixy.setLamp(1, 0);
}
void loop(){
int i;
pixy.ccc.getBlocks();
x= pixy.ccc.blocks[i].m_x; //Getting Ball Coordinate X
y= pixy.ccc.blocks[i].m_y; //Getting Ball Coordinate Y
myValue1.number = getFloat(); // Getting Servo Angle1
myValue2.number = getFloat(); // Getting Servo Angle2
myValue3.number = getFloat(); // Getting Servo Angle3
//Converting Angles values to Analog Values
myValue1.number = map(myValue1.number, 0, 300, 0, 1023);
myValue2.number = map(myValue2.number, 0, 300, 0, 1023);
myValue3.number = map(myValue3.number, 0, 300, 0, 1023);
//Sending Analog value (Corresponding to servo angles)
if( myValue1.number && myValue2.number && myValue3.number != 0)
{
sc.WritePos(1, myValue1.number, 0);
sc.WritePos(2, myValue2.number, 0);
sc.WritePos(3, myValue3.number, 0);
S1 = myValue1.number;
S2 = myValue1.number;
S3 = myValue1.number;
}
else
{
sc.WritePos(1, S1, 0);
sc.WritePos(2, S2, 0);
sc.WritePos(3, S3, 0);
}
//Converting & sending Analog data to Servo Angle
send3.number = map(sc.ReadPos(1), 0, 1023, 0, 300);
send4.number = map(sc.ReadPos(2), 0, 1023, 0, 300);
send5.number = map(sc.ReadPos(3), 0, 1023, 0, 300);
//Sending Ball's Coordinates
send1.number =x; //save PosRead value in send1
send2.number =y; //save PosRead value in send1
Serial.write('A');
// Print float data
for (int i=0; i<4; i++){
Serial.write(send1.bytes[i]); //Send X
}
for (int i=0; i<4; i++){
Serial.write(send2.bytes[i]); //Send Y
}
for (int i=0; i<4; i++){
Serial.write(send3.bytes[i]); //Send Servo Angle
}
for (int i=0; i<4; i++){
Serial.write(send4.bytes[i]); //Send Servo Angle
}
for (int i=0; i<4; i++){
Serial.write(send5.bytes[i]); //Send Servo Angle
}
// Print terminator
Serial.print('\n');
delay(50);
}
float getFloat(){
int cont = 0;
FLOATUNION_t f;
while (cont < 4 ){
f.bytes[cont] = Serial.read() ;
cont = cont +1;
}
return f.number;
}
How can I check if Simulink is sending a longer reset than the IDE?
Thank you!