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

理论篇

代码篇:

opencv环境配置

注意!注意!注意!

配置结束后运行环境切换为如下再运行:

cpp 复制代码
#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;
}

运行结果:

相关推荐
魔法工坊25 分钟前
只谈C++11新特性 - 删除函数
java·开发语言·c++
一勺汤38 分钟前
YOLO11改进-注意力-引入自调制特征聚合模块SMFA
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·目标跟踪
AI服务老曹1 小时前
报警推送消息升级的名厨亮灶开源了
人工智能·安全·开源·音视频
白兔12051 小时前
联邦大模型微调
人工智能·深度学习
海螺姑娘的小魏1 小时前
Effective C++ 条款 26:尽可能延后变量定义式的出现时间
开发语言·c++
全域观察1 小时前
平安夜与圣诞节,如何玩转节日选题?
人工智能·新媒体运营·媒体·内容运营·程序员创富
w(゚Д゚)w吓洗宝宝了1 小时前
C++ 环境搭建 - 安装编译器、IDE选择
开发语言·c++·ide
Ricciflows1 小时前
MIT线性代数教材:Linear Algebra and Its Applications
人工智能·学习·线性代数·机器学习·数学建模·矩阵
凡人的AI工具箱1 小时前
每天40分玩转Django:Django Email
数据库·人工智能·后端·python·django·sqlite
RFID舜识物联网2 小时前
RFID智能文件柜:高效安全的档案管理新方案
大数据·网络·人工智能·嵌入式硬件·物联网