Arduino error lost sync device restatring..

asked 2023-01-28 05:48:42 -0500

jagilamkumar gravatar image

updated 2023-01-28 05:53:42 -0500

hi , when i run my arduino code in previous arduino uno, the code is running well ...after changing the arduino board , when we upload the same code and try to execute , getting error like "Lost sync with device, restarting..."..actually when we run three stepper motors information we are getting issue, but when we eliminated 1 stepper information from the code the code is executing...please tell me the solution..arduino code

`#include <accelstepper.h>

include <ros.h>

include <std_msgs uint16multiarray.h="">

// Define the number of steps per revolution const int stepsPerRevolution = 800; const int gearRatio = 19; const int gearRatio_end = 14;

// Declare the PUL and DIR pins for each motor const int motor1_pul = 2; const int motor1_dir = 3;

const int motor2_pul = 4; const int motor2_dir = 5;

//const int motor3_pul = 6; //const int motor3_dir = 7;

// Create an AccelStepper object for each motor AccelStepper motor1(AccelStepper::DRIVER, motor1_pul, motor1_dir); AccelStepper motor2(AccelStepper::DRIVER, motor2_pul, motor2_dir); //AccelStepper motor3(AccelStepper::DRIVER, motor3_pul, motor3_dir);

// Initialize the ROS node ros::NodeHandle nh;

// Create a message to hold the steps for each motor // std_msgs::UInt16MultiArray steps_msg;

// Callback function for the arduinobot/arm_actuate subscriber void arm_actuate_cb( const std_msgs::UInt16MultiArray& angles_msg){

// Extract the angles from the message int angle1 = angles_msg.data[0]; int angle2 = angles_msg.data[1]; //int angle3 = angles_msg.data[2];

// Calculate the number of steps required to move each motor to the specified angle int steps1 = gearRatio * (angle1 / 360.0) * stepsPerRevolution; int steps2 = gearRatio * (angle2 / 360.0) * stepsPerRevolution; //int steps3 = gearRatio_end * (angle3 / 360.0) * stepsPerRevolution;

if (angle1 >= motor1.currentPosition()) { digitalWrite(motor1_dir, HIGH); } else { digitalWrite(motor1_dir, LOW); } if (angle2 >= motor2.currentPosition()) { digitalWrite(motor2_dir, HIGH); } else { digitalWrite(motor2_dir, LOW); } // if (angle3 >= motor3.currentPosition()) { // digitalWrite(motor3_dir, HIGH); // } else { // digitalWrite(motor3_dir, LOW); //}

// Move the motors the specified number of steps motor1.moveTo(steps1); motor2.moveTo(steps2); //motor3.moveTo(steps3); // steps_msg.data[0] = steps1; // steps_msg.data[1] = steps2; // steps_msg.data[2] = steps3; }

// Define the subscriber to the topic /servo_actuate where are published UInt16MultiArray messages // Define the function that will be triggered each time a new message is published on this topic ros::Subscriber<std_msgs::uint16multiarray> sub("arduino/arm_actuate", &arm_actuate_cb );

void setup(){

// Set the maximum speed and acceleration for each motor motor1.setMaxSpeed(10000); motor1.setAcceleration(5000);

motor2.setMaxSpeed(10000); motor2.setAcceleration(5000);

// motor3.setMaxSpeed(10000); // motor3.setAcceleration(5000);

// Inizialize the ROS node on the Arduino nh.initNode(); // Inform ROS that this node will subscribe to messages on a given topic nh.subscribe(sub); }

void loop() { // Run the motor update functions motor1.run(); motor2.run(); //motor3.run();

// Keep the ROS node up and running nh.spinOnce(); delay(1); }`

edit retag flag offensive close merge delete