机械狗实习项目

项⽬
:
利⽤ Realsense 实现 机械 闭环 控制
内容
,
设计 ⼀个 函数
go-to-positioncintx.int ,
yny-i.cxo.gs
i
o
> x
x
机械 当前 位置 原点
,
调⽤ 函数
,
到达 指定
要求
:
使⽤ c.tt
利⽤ IMU 采⽤ 闭环 控制
误差 尽可能
提示
:
可选 算法
#include<iostream>
#include<algorithm>
#include<cmath>
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
// Start pipeline with chosen configuration
pipe.start(cfg);
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
// Start pipeline with chosen configuration
pipe.start(cfg);

// Main loop
while (true) {
	// Wait for the next set of frames from the camera
	auto frames = pipe.wait_for_frames();
	// Get a frame from the pose stream
	auto f = frames.first_or_default(RS2_STREAM_POSE);
	```

	We cast the frame that arrives to `pose_frame` in order to access its `pose_data`.
	```cpp
// Cast the frame to pose_frame and get its data
	auto pose_data = f.as<rs2::pose_frame>().get_pose_data();
// Main loop
	while (true) {
		// Wait for the next set of frames from the camera
		auto frames = pipe.wait_for_frames();
		// Get a frame from the pose stream
		auto f = frames.first_or_default(RS2_STREAM_POSE);
		```

		We cast the frame that arrives to `pose_frame` in order to access its `pose_data`.
		```cpp
// Cast the frame to pose_frame and get its data
		auto pose_data = f.as<rs2::pose_frame>().get_pose_data();

//平移(以米为单位,相对于初始位置)
//速度(以米/秒为单位)
//旋转(以四元数旋转表示,相对于初始位置)
//角速度(以弧度/秒为单位)
//角加速度(以弧度/秒^2为单位)
//跟踪器置信度(姿势数据置信度0x0 - 失败,0x1 - 低,0x2 - 中,0x3 - 高)
//映射器置信度(姿势数据置信度0x0 - 失败,0x1 - 低,0x2 - 中,0x3 - 高)

// Print the x, y, z values of the translation, relative to initial position
		std::cout << "\r" << "Device Position: " << std::setprecision(3) << std::fixed << pose_data.translation.x << " " <<
		          pose_data.translation.y << " " << pose_data.translation.z << " (meters)";
// Print the x, y, z values of the translation, relative to initial position
		std::cout << "\r" << "Device Position: " << std::setprecision(3) << std::fixed << pose_data.translation.x << " " <<
		          pose_data.translation.y << " " << pose_data.translation.z << " (meters)";

//定义值
		struct PID {
			double XSetSpeed,YSetSpeed;             //定义设定值
			double XActualSpeed,YActualSpeed;       //定义实际值
			double Xerr,Yerr;                       //定义偏差值
			double Xerr_last,Yerr_last;             //定义上一个偏差值
			double Kp,Ki,Kd;                        //定义比例、积分、微分系数
			double voltage;                         //定义电压值(控制执行器的变量)
			double XincrementSpeed,YincrementSpeed; //定义积分值
		} pid;

		cin >> pid.Kp >>pid.Ki >>pid.Kd;

//输入目的地坐标值    传输当前坐标值  若不在该坐标值上,则以直线:y=kx为路径前往目的地
		double x,y,x0,y0;  //xo,yo为初始坐标值;x,y为目标坐标值
		cin >>x0 >>y0 >>x >>y;

//机械狗每走一步IMU判断一次机械狗位置
//利用PID算法将偏差值缩小至无限接近于0 ,修正机械狗行走的路线
		double frequency,stride,P;         //机械狗行走的 频率 ,步幅
		int n,P=0;                         //定义 一个机械狗行走的时间函数 ,P随之 增加
		while(P%frequency==0&&fabs(x-x0)>0.01&&fabs(y-y0)>0.01) { /*0.01米*/
			//Xspeed ,Yspeed 由IMU传入
			n=p%frequency;
			double PID_realize(double Xspeed,double Yspeed) {
				// y=kx
				pid.XSetSpeed=x*n/stride;             //这四个值是实时改变的
				pid.YSetSpeed=k*x*n/stride;

				pid.XActualSpeed=Xspeed;
				pid.YActualSpeed=Yspeed;

				pid.Xerr=pid.XSetSpeed-pid.XActualSpeed;
				pid.Yerr=pid.YSetSpeed-pid.YActualSpeed;

				//位置型控制算法  微积分
				XincrementSpeed=pid.Kp*(pid.Xerr-pid.Xerr_next)+pid.Ki*pid.Xerr+pid.Kd*(pid.Xerr-2*pid.Xerr_next+pid.Xerr_last);
				YincrementSpeed=pid.Kp*(pid.Yerr-pid.Yerr_next)+pid.Ki*pid.Yerr+pid.Kd*(pid.Yerr-2*pid.Yerr_next+pid.Yerr_last);

				pid.XActualSpeed+=XincrementSpeed;
				pid.YActualSpeed+=YincrementSpeed;

				pid.Xerr_last=pid.Xerr_next;
				pid.Yerr_last=pid.Yerr_next;
				pid.Xerr_next=pid.Xerr;
				pid.Yerr_next=pid.Yerr;

				return pid.XActualSpeed,pid.YActualSpeed;      //返回经修正后需实际到达的坐标点
			}
		}

//使最终机械狗离目的地误差不超过0.01米

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDsoup

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值