We use the ultrasonic module to access Arduino,
Connection trig port 2, echo Access 3 port.
Const intTrigpin =2;Const intEchopin =3; intcm;unsignedLongTime_ms =0;voidSetup () {Serial.begin (9600); Pinmode (Trigpin, OUTPUT); Pinmode (Echopin, INPUT);}voidLoop () {Digitalwrite (Trigpin, low);//low and high levels send a short time pulse to TrigpinDelaymicroseconds (1); Digitalwrite (Trigpin, high); Delaymicroseconds (1); Digitalwrite (Trigpin, low); CM= Pulsein (Echopin, High)/58.0;//Convert echo time to cmCM = (int(CM *100.0)) /100.0;//keep two decimal placesTime_ms =Millis ();//Serial.print (Time_ms);//serial.println ("");serial.print (cm); Serial.println ("");d Elay ( -); }
View Code
The USB print is displayed as an int.
#include <ros/ros.h>#include"geometry_msgs/twist.h"#include<boost/asio.hpp>//include boost library functions#include <boost/bind.hpp>#include<sstream>#include<string>using namespacestd;using namespaceboost::asio;unsignedCharbuf[ -];intMainintargcChar**argv) {Ros::init (argc, argv,"Echo"); Ros::nodehandle N; Ros::P ublisher echo_msg= N.advertise<geometry_msgs::twist> ("Echo/stop", +); Io_service io; Serial_port sp (IO,"/dev/ttyacm0");//define the serial port of the transmissionSp.set_option (Serial_port::baud_rate (9600));//baud rateSp.set_option (Serial_port::flow_control (Serial_port::flow_control::none));//Flow ControlSp.set_option (serial_port::p arity (serial_port::p arity::none));//Parity CheckSp.set_option (Serial_port::stop_bits (Serial_port::stop_bits::one));//Stop BitSp.set_option (Serial_port::character_size (8) );//Data bitsros::rate loop_rate ( -); intnum; intRead_num =0 ; intFlag =0; while(Ros::ok ()) {intnum =0; Charbuf[1]; Boost::system::error_code err; Read (SP, buffer (BUF)); if(Err) {Ros_info ("Serial Port Read_some error!"); return-1; } if(buf[0] =='\ n') {printf ("%d \ n", read_num/Ten); Read_num=0; }Else{Std::stringstream ss; SS<< buf[0]; SS>>num; Read_num= Read_num *Ten+num; } geometry_msgs::twist Velo; if(read_num/Ten<= -){ //Publish a msg}Else{} loop_rate.sleep (); } sp.close (); return 0;}
Finally combined with previous Ubuntu read USB, read the distance and incoming data.
USB access is complete.
Introduction to Ros (iii) Ubuntu Access USB Read ultrasonic ranging