Robot programming introduction (C ++ / Arduino / ROS) (reprint edition 2020)

In this paper, robot programming introduction of installation and configuration, and demonstrates through simple examples are based on Arduino and ROS character building machines networked system prototype. ROS and Arduino support in C ++ programming.

 

Robot programming introduction (C ++ / Arduino / ROS)

ROS + Arduino

installation

Recommended installation environment windows10, of course, also supports MacOS and Linux, in order to ensure versatility to the Windows environment as the default configuration.

 

Robot programming introduction (C ++ / Arduino / ROS)

ONE

 

1.1 Download and install Arduino

The latest version arduino-1.8.10-windows, double-click the downloaded installation package (administrator privileges).

Robot programming introduction (C ++ / Arduino / ROS)

Consent agreement

 

Robot programming introduction (C ++ / Arduino / ROS)

Select Components

 

Robot programming introduction (C ++ / Arduino / ROS)

installation manual

If you need to change, make changes in this step and click Install:

 

Robot programming introduction (C ++ / Arduino / ROS)

Wait for the end of the installation

 

Robot programming introduction (C ++ / Arduino / ROS)

Click Close to complete installation

Double-click the desktop icon Arduino, you can open the IDE program design.

 

Robot programming introduction (C ++ / Arduino / ROS)

Arduino IDE

1.2 Installation ROS

Robot operating system ROS, can be understood as the Internet of Things (IoT) system used in a particular field of robotics , a lot of knowledge and skills like communication, learn and master this tool is very important. ROS rich interface, can be combined with OpenCV, OpenAI other third party open source tools to develop a novel and practical kinds of robots.

Windows of ROS1 & 2 requires 64-bit desktop version of Windows 10 or Windows 10 IoT things Enterprise Edition.

  • Before you proceed, please clean up and backup c: \ data under all existing opt.
  • c: \ opt was specified install location. The current relocation is not enabled. It does not support installation in a different location! ! !
  • Make sure the C: \ drive has a 10 GB of free space for the installation and development.
  • Install Visual Studio 2019
  • Install the package manager
  • Installation Git: choco upgrade git -y
  • Installation ROS 1 Melodic or ROS 2 Dashing, where ROS 1 to Case.
choco source add -n=ros-win -s="https://roswin.azurewebsites.net/api/v2" --priority=1
choco upgrade ros-melodic-desktop_full -y --execution-timeout=0

Create a shortcut to launch ROS:

C:\Windows\System32\cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" -arch=amd64 -host_arch=amd64 && c:\opt\ros\melodic\\x64\setup.bat

Update ROS:

choco upgrade ros-melodic-desktop_full -y --execution-timeout=0

test

2.1 Arduino program

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

As described above, pin 13 is Arduino flashing LED lamp program

 

Robot programming introduction (C ++ / Arduino / ROS)

One

Compile and upload it to Arduino:

 

Robot programming introduction (C ++ / Arduino / ROS)

LED13 light flashes

everything is normal!

2.2 ROS

Start roscore, then start turtlesim.

 

Robot programming introduction (C ++ / Arduino / ROS)

rosrun

 

Robot programming introduction (C ++ / Arduino / ROS)

turtlesim

everything is normal!

Examples

Arduino the A0 / A1 pins are connected to the slide resistor and photoresistor. Using the following procedure to send the value to the computer terminal:

Open the Arduino IDE, select Tools -> Manage Libraries ......

 

Robot programming introduction (C ++ / Arduino / ROS)

rosserial library

Then, open the File -> Examples -> Examples of third-party libraries -> Rosserial Arduino Library-> ADC:

/* 
 * rosserial ADC Example
 * 
 * This is a poor man's Oscilloscope.  It does not have the sampling 
 * rate or accuracy of a commerical scope, but it is great to get
 * an analog value into ROS in a pinch.
 */

#if (ARDUINO >= 100)
 #include <Arduino.h>
#else
 #include <WProgram.h>
#endif
#include <ros.h>
#include <rosserial_arduino/Adc.h>

ros::NodeHandle nh;

rosserial_arduino::Adc adc_msg;
ros::Publisher p("adc", &adc_msg);

void setup()
{ 
  pinMode(13, OUTPUT);
  nh.initNode();

  nh.advertise(p);
}

//We average the analog reading to elminate some of the noise
int averageAnalog(int pin){
  int v=0;
  for(int i=0; i<4; i++) v+= analogRead(pin);
  return v/4;
}

long adc_timer;

void loop()
{
  adc_msg.adc0 = averageAnalog(0);
  adc_msg.adc1 = averageAnalog(1);
  adc_msg.adc2 = averageAnalog(2);
  adc_msg.adc3 = averageAnalog(3);
  adc_msg.adc4 = averageAnalog(4);
  adc_msg.adc5 = averageAnalog(5);
    
  p.publish(&adc_msg);

  nh.spinOnce();
}

The effect of this code is all Arduino AD port ROS message formats to transmit data to the computer terminal, the compiler upload.

Here, it is known Arduino download port COM3, after opening roscore, start the following command:

rosrun rosserial_python serial_node.py COM3

 

Robot programming introduction (C ++ / Arduino / ROS)

Open rosserial_python use COM3 port

Use rostopic list, check out:

 

Robot programming introduction (C ++ / Arduino / ROS)

/adc

The theme / adc value is displayed:

 

Robot programming introduction (C ++ / Arduino / ROS)

Analog data

Use rqt_plot:

 

Robot programming introduction (C ++ / Arduino / ROS)

Sensor data curve

to sum up

Herein achieved under windows10 system for Arduino and ROS programming installation and configuration, and the exemplary C ++ program presentation, transmitting microcontroller sensor data to the computer terminal and making the numerical value curve drawing tool, more details and detailed, will subsequent content, and gradually introduced.

Published 472 original articles · won praise 1300 · Views 2.14 million +

Guess you like

Origin blog.csdn.net/ZhangRelay/article/details/104668711