用自己的机器人和ubuntu PC实现通信和控制--26
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/
前提: 1.拥有一台能够采用手动或者自动移动的机器人移动平台。
2.在电机端需要安装高分辨率的霍尔编码器。
3.在终端控制板上有基本的电机PWM控制,PID电机补偿调速。编码器输入捕获,串口数据收发的驱动。
以下是我的机器人:

控制思路如下:

1.电机驱动板获取编码器的数据通过CAN总线透传给地盘主控。地盘主控解析编码器的数据计算出X方向上的线速度和Z轴上的角速度,通过串口上传到ubuntuPC机上。源码如下:(采用定时器方式计算速度)
struct Encoder_{
float d_left;
float d_right;int enc_left; //wheel encoder readings
int enc_right;
int left; // actual values coming back from robot
int right;
}self;
float d,th;
#define ticks_meter 123077.0 //每米编码器的值 linear = 2.6
#define base_width 0.31f; //轴距 angule = 0.316
#define robot_timer 0.53f //周期
union Max_Value
{
unsigned char buf[12];
struct _Float_{
float hander;
float _float_vx;
float _float_vth;
}Float_RAM;
}Send_Data;
extern int encoder_0;
extern int encoder_1;
void Updata_velocities_Data()
{
u8 i=0;
self.right = encoder_0;//编码器的累计量
self.left = encoder_1;
if(self.enc_left == 0)
{
self.d_left = 0;
self.d_right = 0;
}
else
{
self.d_left = (self.left - self.enc_left) / ticks_meter;
self.d_right = (self.right - self.enc_right) / ticks_meter;
}
self.enc_left = self.left;
self.enc_right = self.right;
d = ( self.d_left + self.d_right ) / 2; //distance traveled is the average of the two wheels
th = ( self.d_right - self.d_left ) / base_width; //this approximation works (in radians) for small angles
self.dx = d / robot_timer; //calculate velocities
self.dr = th / robot_timer;
Send_Data.Float_RAM.hander = 15.5;
Send_Data.Float_RAM._float_vx = self.dx;
Send_Data.Float_RAM._float_vth = self.dr;
for(i=0;i<12;i++)
sendchar_usart1(Send_Data.buf[i]);
}
2.在ubuntu PC端通过boost串口接收数据时。对串口进行读取 即可。在此节点中我们需要发布从下位机获取的速度信息,还要监听/cmd_vel话题下的移动数据,发送到下位机。代码如下:
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>
#include <iostream>
#include <iomanip>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <math.h> using namespace std;
using namespace boost::asio; ros::Time current_time, last_time;
double x = 0.0;
double y = 0.0;
double th = 0.0;
double vx = 0.0;
double vy = 0.0;
double vth = 0.0; union _SPEED_
{
unsigned char speed_buf[16];
struct _speed_value_
{
float flag;
float left_vel;
float yspeed_vel;
float right_vel;
}Struct_Speed;
}Union_Speed; geometry_msgs::Quaternion odom_quat; void cmd_velCallback(const geometry_msgs::Twist &twist_aux)
{
geometry_msgs::Twist twist = twist_aux;
Union_Speed.Struct_Speed.flag = 15.5;
Union_Speed.Struct_Speed.left_vel = twist_aux.linear.x;
Union_Speed.Struct_Speed.yspeed_vel = 0.0;
Union_Speed.Struct_Speed.right_vel = twist_aux.angular.z;
} double dt = 0.0; union _Data_
{
unsigned char buf_rev[12];
struct _value_encoder_
{
float Flag_Float;
float VX_speed;
float VZ_speed;
}Struct_Encoder;
}Union_Data; int main(int argc, char** argv)
{
unsigned char check_buf[1];
unsigned char i;
io_service iosev;
serial_port sp(iosev, "/dev/ttyUSB0");
sp.set_option(serial_port::baud_rate(115200));
sp.set_option(serial_port::flow_control(serial_port::flow_control::none));
sp.set_option(serial_port::parity(serial_port::parity::none));
sp.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
sp.set_option(serial_port::character_size(8));
ros::init(argc, argv, "base_controller");
ros::NodeHandle n;
ros::Subscriber cmd_vel_sub = n.subscribe("cmd_vel", 50, cmd_velCallback);
ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
tf::TransformBroadcaster odom_broadcaster;
while(ros::ok())
{
current_time = ros::Time::now();
ros::spinOnce();
dt = (current_time - last_time).toSec();
last_time = ros::Time::now();
current_time = ros::Time::now();
read(sp, buffer(Union_Data.buf_rev));
if(Union_Data.Struct_Encoder.Flag_Float == 15.5)
{
vx = Union_Data.Struct_Encoder.VX_speed;
vth = Union_Data.Struct_Encoder.VZ_speed;
ROS_INFO("msg_encoder.angular_z is %f",vth);
ROS_INFO("msg_encoder.linear.x is %f",vx);
}
else
{
ROS_INFO("Fucking communication fails,The fuck can i hurry up to restart!");
read(sp, buffer(check_buf));
}
write(sp, buffer(Union_Speed.speed_buf,16));
double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
double delta_th = vth * dt;
x += delta_x;
y += delta_y;
th += delta_th;
geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
geometry_msgs::TransformStamped odom_trans;
odom_trans.header.stamp = current_time;
odom_trans.header.frame_id = "odom";
odom_trans.child_frame_id = "base_link"; odom_trans.transform.translation.x = x;
odom_trans.transform.translation.y = y;
odom_trans.transform.translation.z = 0.0;
odom_trans.transform.rotation = odom_quat;
odom_broadcaster.sendTransform(odom_trans);
nav_msgs::Odometry odom;
odom.header.stamp = current_time;
odom.header.frame_id = "odom";
odom.pose.pose.position.x = x;
odom.pose.pose.position.y = y;
odom.pose.pose.position.z = 0.0;
odom.pose.pose.orientation = odom_quat;
odom.child_frame_id = "base_link";
odom.twist.twist.linear.x = vx;
odom.twist.twist.linear.y = vy;
odom.twist.twist.angular.z = vth;
odom_pub.publish(odom);
last_time = current_time;
}
iosev.run();
}
然后测试如下:打开你的机器人,和ubuntu PC 确保串口线链接。然后运行所需的节点:
roscore &
rosrun odom_tf_package tf_broadcaster_node
rviz
rosrun rbx1_nav timed_out_and_back.py //此节点为turtlebot的damo
在tf_broadcaster_node节点下的调试输出如下:

rviz视图中查看机器人入下:

用自己的机器人和ubuntu PC实现通信和控制--26的更多相关文章
- 基于FPGA的红外遥控解码与PC串口通信
基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...
- PC蓝牙通信C#代码实现
PC蓝牙通信C#代码实现 这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下 ...
- 小黄鸡机器人和小I机器人的调用
<?php //---------------------------------聊天小机器人类---------------------------------------------- ...
- 18-09-13 机器人和服务器之间的ip配置和脚本的重启
问题9 服务器安装完毕后 怎么配置机器人客户端的配置ip
- ROS----TUT-RIM协作机器人和Actin-ROS接口
TUT-RIM: Collaborative Intelligent Heavy Machinery and Robotics http://www.ros.org/news/2017/02/tutr ...
- 30行python让图灵机器人和茉莉机器人无止尽的瞎扯蛋
首先注册申请图灵机器人的API: http://www.tuling123.com/ 查看一下API的格式,很简单: { "key": "APIKEY", &q ...
- 公司内网机器vm ubuntu proxy 设置
解决浏览器上网问题: System Setting -> Network -> Network Proxy设置公司的代理 解决apt联网问题: 在/etc/apt/apt.conf文件里加 ...
- Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总
参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: #安装依赖库 sudo apt-get install ...
- 八、mini2440裸机程序之UART(2)UART0与PC串口通信【转】
转自:http://blog.csdn.net/shengnan_wu/article/details/8309417 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.相关原理图 2.相关寄 ...
随机推荐
- IT公司100题-15-求二元查找树的镜像
问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/ \4 12/ \ / \2 5 8 16 输出: 6/ ...
- IT公司100题-11-求二叉树中节点的最大距离
问题描述: 写程序,求一棵二叉树中相距最远的两个节点之间的距离. 10/ \6 14/ \ / \4 8 12 16 分析: 二叉树中最远的两个节点,要么是根 ...
- PHP和.NET通用的加密解密函数类,均使用3DES加解密 .
以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code&q ...
- c#读取文本文档实践2-计算商品价格
商品 数量 单价英语 66 100语文 66 80数学 66 100化学 66 40物理 66 60 上面是文本文档中读入的数据. using System; using System.Collect ...
- Xp 消息队列的使用
1.安装消息队列3.0: 控制面板/添加删除程序/添加window组件/找到消息队列/选择->详细信息->MSMQ HTTP支持. 注意:如果计算机没有连接到域需要去掉Active Dir ...
- 使用siege进行Web压力测试
因为最近公司线上node项目出一些不稳定的情况,考虑在这方面能不能做进一步的优化,既然要做优化首先要知道问题出在哪里? 因为暂无法定位是node层问题还是api层出现问题,由于在开发环境小并发量程序运 ...
- 在T-SQL中访问远程数据库(openrowset、opendatasource、openquery)
1. 启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前要先启用Ad Hoc Distributed Queries服务,因为这个服 ...
- 欢迎参加MVP主讲的Windows 10开发线上课程
博客地址:http://blog.csdn.net/FoxDave Windows 10 Developer Readiness - Powered by MVPs - 由微软最有价值专家(MVP)主 ...
- Tab的键的妙用
vs2013输入“(”的时候自动加入了“)”,开始的时候感觉相当不方便,要按“End”才能继续输入“:”,后来发现按“Tab"也会自动跳出括号,于是满心喜欢.
- for循环进阶
[引例] 输出一行10个“*” #include<cstdio> int main(){ printf("**********\n"); ; } 思考: (1)输出一行 ...