0
$\begingroup$

Rosanswers logo

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++;
}
 } 
}

Originally posted by Mekateng on ROS Answers with karma: 85 on 2017-10-15

Post score: -1


Original comments

Comment by NEngelhard on 2017-10-15:
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.

Comment by Mekateng on 2017-10-15:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by ahendrix with karma: 47576 on 2017-10-15

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Mekateng on 2017-10-15:
Yes but there is a servo motor example in this tutorial, there is not a dc motor example

Comment by ahendrix on 2017-10-15:
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.

Comment by Mekateng on 2017-10-20:
I tried it but unfortunately couldn't do it . Please help me Mr. @ahendrix

Comment by ahendrix on 2017-10-20:
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.

Comment by Mekateng on 2017-10-21:
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

Comment by ahendrix on 2017-10-21:
Have you tried modifying the servo example by replacing the servo.write(); function with a function that sets your motor speed?

Comment by Mekateng on 2017-10-21:
That's what I want to do, but I can not. @ahendrix This code is getting angle data like the same servo.

Comment by ahendrix on 2017-10-21:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.