ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
-1

How to communicate an arduino code with ROS? [closed]

asked 2017-10-15 06:48:07 -0500

Mekateng gravatar image

Hi , I want to commicate this arduino code with ROS. How can I do this ? Please help me ?

#include <Wire.h>
#define MD22ADDRESS         0x58
#define SOFTREG             0x07 
#define MOTOR1              0x01 
#define ACCELLREG           0x03  
signed long int  pulse=0;
unsigned long last_time;
int ang=0;
int encoder_a=2;
int encoder_b=3;
float encoder_resolution=1024;
void setup()
{
Wire.begin();
delay(100);  
Serial.begin(115200); 
attachInterrupt(0,encoder_kesme_a,CHANGE); 
last_time=millis();
setMode();
}
void loop()
{
delay(10);
Wire.beginTransmission(MD22ADDRESS);  
Wire.write(MOTOR1);
Wire.write(220);
Wire.endTransmission();
if(millis()-last_time>1000)
{
ang=((pulse/encoder_resolution)*360.0); 
ang=int(ang)%360; 
Serial.println("Dc Motor Angle : " +String(ang));
last_time=millis(); 
 } 
}
void setMode(){
Wire.beginTransmission(MD22ADDRESS);
Wire.write(ACCELLREG);
Wire.write(0xFF);
Wire.endTransmission();
}
void encoder_kesme_a()
{
if (digitalRead(encoder_a) == HIGH) 
{ 
if (digitalRead(encoder_b) == HIGH) 
{
pulse++ ;  
 }
 else
 {
pulse = pulse - 1; 
}
}
else
 {
if (digitalRead(encoder_b) == HIGH) 
{ 
pulse = pulse - 1; 
 } 
else 
 {
pulse++;
}
 } 
}
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by Mekateng
close date 2017-11-20 04:03:24.378410

Comments

Please add what you tried so far. Your post does not show any attempt of you to solve the task yourself. answers.ros is about helping, not doing it for you.

NEngelhard gravatar image NEngelhard  ( 2017-10-15 10:29:13 -0500 )edit

I converted 2d laserscan to 3d pointcloud and ı want to use dc motor with encoder to angle data instead of dynamixel servo motor . So ı wrote an arduino code that obtain an angle data. But I don't know how to communicate this code with the ROS. How to make the edits on this code.

Mekateng gravatar image Mekateng  ( 2017-10-15 10:40:23 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2017-10-15 12:55:53 -0500

ahendrix gravatar image

You can communicate arduino to ROS using rosserial_arduino. I suggest you start with the tutorials.

edit flag offensive delete link more

Comments

Yes but there is a servo motor example in this tutorial, there is not a dc motor example

Mekateng gravatar image Mekateng  ( 2017-10-15 14:43:59 -0500 )edit
2

Yes, this is not a copy of the servo example, but the principle of subscribing to a topic and updating a hardware setting based on that data is fundamentally the same.

ahendrix gravatar image ahendrix  ( 2017-10-15 14:53:17 -0500 )edit

I tried it but unfortunately couldn't do it . Please help me Mr. @ahendrix

Mekateng gravatar image Mekateng  ( 2017-10-20 15:57:00 -0500 )edit
1

As @NEngelhard said, we really need to know what you've tried and how it isn't working in order to help. We're not here to do the work for you.

ahendrix gravatar image ahendrix  ( 2017-10-20 17:31:06 -0500 )edit

Normally, There is an example of the servomotor communicating with the ROS in the rosserial tutorials. But ı have dc motor with encoder. I want to communicate ROS with dc motor which is code above. I am asking how to make ROS communication with the above code. @ahendrix

Mekateng gravatar image Mekateng  ( 2017-10-21 04:28:57 -0500 )edit

Have you tried modifying the servo example by replacing the servo.write(); function with a function that sets your motor speed?

ahendrix gravatar image ahendrix  ( 2017-10-21 11:46:55 -0500 )edit

That's what I want to do, but I can not. @ahendrix This code is getting angle data like the same servo.

Mekateng gravatar image Mekateng  ( 2017-10-21 15:19:18 -0500 )edit
1

Saying that you cannot does not make it like you've tried. Please edit your question with the code that you're trying to use that includes your ROS subscriber and describe why it doesn't compile or why it doesn't work.

ahendrix gravatar image ahendrix  ( 2017-10-21 15:24:50 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-15 06:48:07 -0500

Seen: 171 times

Last updated: Oct 15 '17