Robotics StackExchange | Archived questions

Code Arduino to ROS

Hi, I have my robot Arduino and i want to command it with ROS. I install ROS and Rosserial_arduino in my raspberry and i want to command my Robot. can you help me please to add the instructions of ROS in my code arduino? my code is in this page: https://electronics4all.wordpress.com/2014/02/09/first-obstacle-avoidance-robot/ Thanks

Asked by Emilien on 2016-03-28 09:10:57 UTC

Comments

Please post the code here. Otherwise the answer will become useless for others if the link to the blog does not work anymore.

Asked by NEngelhard on 2016-03-28 09:59:52 UTC

Answers

#include

include

include

define TRIG_PIN A4 // Pin A4 on the Motor Drive Shield soldered to the ultrasonic sensor

define ECHO_PIN A5 // Pin A5 on the Motor Drive Shield soldered to the ultrasonic sensor

define MAX_DISTANCE 200 // sets maximum useable sensor measuring distance to 200cm

define MAX_SPEED 180 // sets speed of DC traction motors to 180/256 or about 70% of full speed - to get power drain down.

define MAX_SPEED_OFFSET 10 // this sets offset to allow for differences between the two DC traction motors

define COLL_DIST 10 // sets distance at which robot stops and reverses to 10cm

define TURN_DIST COLL_DIST+10 // sets distance at which robot veers away from object (not reverse) to 20cm (10+10)

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to measure distance.

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency AF_DCMotor motor2(4, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency Servo myservo; // create servo object to control a servo

int pos = 0; // this sets up variables for use in the sketch (code) int maxDist = 0; int maxAngle = 0; int maxRight = 0; int maxLeft = 0; int maxFront = 0; int course = 0; int curDist = 0; String motorSet = ""; int speedSet = 0;

//-------------------------------------------- SETUP LOOP ---------------------------------------------------------------------------- void setup() { myservo.attach(9); // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward. delay(2000); // delay for two seconds checkPath(); // run the CheckPath routine to find the best path to begin travel motorSet = "FORWARD"; // set the director indicator variable to FORWARD myservo.write(90); // make sure servo is still facing forward moveForward(); // run function to make robot move forward } //------------------------------------------------------------------------------------------------------------------------------------

//---------------------------------------------MAIN LOOP ------------------------------------------------------------------------------ void loop() { checkForward(); // check that if the robot is supposed to be moving forward, that the drive motors are set to move forward - this is needed to overcome some issues with only using 4 AA NiMH batteries checkPath(); // set ultrasonic sensor to scan for any possible obstacles }

Asked by Emilien on 2016-03-28 10:17:52 UTC

Comments

and i have also all function of this code

Asked by Emilien on 2016-03-28 10:18:47 UTC

Please edit your question instead of posting something as an answer. What have your tried so far? What do you want? Where is your problem?

Asked by NEngelhard on 2016-03-28 10:53:45 UTC

i want to add instruction of ROS into my code

Asked by Emilien on 2016-03-28 11:25:14 UTC

That's a bit inexact. Why? What kind of instructions? What do you want to achieve?

Asked by NEngelhard on 2016-03-28 12:57:36 UTC

i have arduino robot with the code all is ok. i want to command it with arduino. so i installed ROS in raspberry and rosserial. now, i want to converte my code arduino to code arduino with ROS.

Asked by Emilien on 2016-03-28 14:59:25 UTC

But where is your problem? This is a forum where you can ask for help if you are stuck and no place where others are doing the work for you.

Asked by NEngelhard on 2016-03-29 01:46:40 UTC