当前位置: 首页 > article >正文

速度规划:s形曲线------pencv c++绘图(1)

理论篇

代码篇:

opencv环境配置
注意!注意!注意!
配置结束后运行环境切换为如下再运行:
在这里插入图片描述

#include <iostream>
#include <cmath>

#include <opencv2/opencv.hpp>

using namespace std;

double a_max = 1.5; // 最大加速度
double v_max = 6.0; // 最大速度
double J = 1; // 最大速度
double T4 = 2.0;

double T1 = a_max / J;
double T3 = T1; 
double T5 = T3;
double T7 = T5;

double T2 = v_max / a_max - T1; 
double T6 = T2;

double t1 = T1;
double t2 = t1 + T2;
double t3 = t2 + T3;
double t4 = t3 + T4;
double t5 = t4 + T5;
double t6 = t5 + T6;
double t7 = t6 + T7;

double a1 = J * T1;//该段最后时刻的速度
double a2 = a_max;
double a3 = 0;
double a4 = 0;
double a5 = a_max - J * T5;
double a6 = -a_max;
double a7 = 0;


double v1 = 0.5 * J * pow(T1, 2);//该段最后时刻的速度
double v2 = v1 + J * T1 * T2;
double v3 = v2 + 0.5 * J * pow(T1, 2);
double v4 = v3;
double v5 = v4 - 0.5 * J * pow(T5, 2);
double v6 = v5 - J * T5 * T6;
double v7 = v6 - 0.5 * J * pow(T5, 2);

double S1 = J * pow(T1, 3) / 6.0;
double S2 = S1 + v1 * T2 + J * T1 * pow(T2, 2) * 0.5;
double S3 = S2 + v2 * T3 + J * T1 * pow(T3, 2) * 0.5 - J * pow(T3, 3) / 6;
double S4 = S3 + v3 * T4;
double S5 = S4 + v4 * T5 - J * pow(T5, 3) / 6;
double S6 = S5 + v5 * T6 - J * T1 * pow(T6, 2) * 0.5;
double S7 = S6 + v6 * T7 - J * T1 * pow(T7, 2) * 0.5 + J * pow(T7, 3) / 6;


double VelocityCurve(double t) {
    double temp = 0;
    if (t < 0 || t >= t7) {
        return 0;
    }
    else if (t < t1) {
        return 0.5 * J * pow(t, 2);
    }
    else if (t < t2) {
        temp = t - t1;
        return v1 + J * T1 * temp;
    }
    else if (t < t3) {
        temp = t - t2;
        return v2 + J * T1 * temp - 0.5 * J * pow(temp, 2);
    }
    else if (t < t4) {
        temp = t - t3;
        return v3;
    }
    else if (t < t5) {
        //std::cout << "t5时刻的具体时间: " << t << "    ";
        temp = t - t4;
        return v4 - 0.5 * J * pow(temp, 2);
    }
    else if (t < t6) {
        //std::cout << "t6时刻的具体时间: " << t << "    ";
        std::cout << "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss: " << t << "    ";
        temp = t - t5;
        return v5 - J * T5 * temp;
    }
    else if (t < t7) {
        //std::cout << "t7时刻的具体时间: " << t << "    ";
        temp = t - t6;
        return v6 - J * T5 * temp + 0.5 * J * pow(temp, 2);
    }
}




double PathCurve(double t) {
    double temp = 0;
    if (t < 0) {
        return 0;
    }
    else if (t >= t7) {
        return S7;
    }
    else if (t < t1) {
        return J * pow(t, 3) / 6.0;
    }
    else if (t < t2) {
        temp = t - t1;
        return S1 + v1 * temp + J * T1 * pow(temp, 2) * 0.5;
    }
    else if (t < t3) {
        temp = t - t2;
        return S2 + v2 * temp + J * T1 * pow(temp, 2) * 0.5 - J * pow(temp, 3) / 6;
    }else if (t < t4) {
        temp = t - t3;
        return S3 + v3 * temp;
    }
    else if (t < t5) {
        std::cout << "t5时刻的具体时间: " << t << std::endl;
        temp = t - t4;
        return S4 + v4 * temp - J * pow(temp, 3) / 6;
    }
    else if (t < t6) {
        std::cout << "t6时刻的具体时间: " << t << std::endl;
        temp = t - t5;
        return S5 + v5 * temp - J * T1 * pow(temp, 2) * 0.5;
    }
    else if (t < t7) {
        std::cout << "t7时刻的具体时间: " << t << std::endl;
        temp = t - t6;
        return S6 + v6 * temp - J * T1 * pow(temp, 2) * 0.5 + J * pow(temp, 3) / 6;
    }
}

int main() {
    double tf = 16.0; // 总时间

    cv::Mat canvas(400, 400, CV_8UC3, cv::Scalar(255, 255, 255)); // 创建一个300x300像素的画布
    
    // 显示第一个圆
    cv::line(canvas, cv::Point(0, 0), cv::Point(0, 400), cv::Scalar(255, 0, 0), 2);//y周  (x,y)
    cv::line(canvas, cv::Point(0, 0), cv::Point(400, 0), cv::Scalar(255, 0, 0), 2);//x周  (x,y)
    
    bool flag = false;
    double last_st = 0, last_t = 0;
    int kx = 30, ky = 10;
    for (double t = 0.0; t <= tf; t += 0.1) {
        //double s_t = PathCurve(t);
        double s_t = VelocityCurve(t);
        if (!flag) {
            cv::circle(canvas, cv::Point(t * kx, s_t * ky), 2, cv::Scalar(0, 0, 255), -1);
        }
        else {
            cv::circle(canvas, cv::Point(t * kx, s_t * ky), 2, cv::Scalar(0, 0, 255), -1);
            cv::line(canvas, cv::Point(last_t * kx, last_st * ky), cv::Point(t * kx, s_t * ky), cv::Scalar(255, 0, 0), 1);//y周  (x,y)
    
        }
        last_st = s_t;
        last_st = t;
    
        std::cout <<t<< "时刻的位置 = " << s_t << std::endl;
    }
    cout << "t5:" << t5 << endl;
    cout << "t6:" << t6 << endl;
    cout << "t7:" << t7 << endl;
    
    // 创建镜像图像矩阵  
    cv::Mat mirror_img;
    cv::flip(canvas, mirror_img, 0);  // 水平镜像,flipCode=1  
    
    // 显示原始图像和镜像图像  
    //cv::imshow("Original Image", canvas);
    cv::imshow("Image", mirror_img);
    //cv::waitKey(0);
    
    cv::waitKey(); // 等待10秒



    return 0;
}

运行结果:
请添加图片描述


http://www.kler.cn/a/229597.html

相关文章:

  • HOW - Form 表单确认校验两种模式(以 Modal 场景为例)
  • 校园跑腿小程序---轮播图,导航栏开发
  • 信息科技伦理与道德3:智能决策
  • 用户界面的UML建模11
  • 单片机-定时器中断
  • Unity + Firebase + GoogleSignIn 导入问题
  • LeetCode--455.分发饼干
  • SpringBoot之整合PageHelper分页插件
  • Android rom定制 修改system分区的容量大小
  • Kotlin手记(一):基础大杂烩
  • Spring MVC跨域设置
  • mac检查CPU温度和风扇速度软件:Macs Fan Control Pro 1.5.17中文版
  • RabbitMQ_00000
  • docker部署docker运维工具
  • MongoDB从入门到实战之Docker快速安装MongoDB
  • Go语言每日一练 ——链表篇(三)
  • XGB-3: 模型IO
  • [UI5 常用控件] 06.Splitter,ResponsiveSplitter
  • node环境打包js,webpack和rollup两个打包工具打包,能支持vue
  • SpringBoot中使用Spring自带线程池ThreadPoolTaskExecutor与Java8CompletableFuture实现异步任务示例
  • YOLOv8改进 | 检测头篇 | 独创RFAHead检测头超分辨率重构检测头(适用Pose、分割、目标检测)
  • 深度强化学习基础【1】-动态规划问题初探(leetcode算法的63题-不同路径II)
  • 题目:有1,2,3,4共四个数字,能组成多少个不相同而且无重复数字的三位数有多少个,都是多少?lua
  • 忘记 RAG:拥抱Agent设计,让 ChatGPT 更智能更贴近实际
  • 【数据结构和算法】--- 基于c语言排序算法的实现(1)
  • Elasticsearch:基本 CRUD 操作 - Python