S7曲线代码

S7曲线

在这里插入图片描述

电机S7曲线加加速 匀加速 减加速 匀速 加减速 匀减速 减减速代码

#include <iostream>
#include <cmath>

// 定义电机参数
const double maxSpeed = 100.0; // 最大速度
const double acceleration = 10.0; // 加速度
const double deceleration = 10.0; // 减速度

// S7曲线加加速
void s7CurveAcceleration(double targetSpeed) {
    double currentSpeed = 0.0;
    double time = 0.0;
    
    while (currentSpeed < targetSpeed) {
        currentSpeed += acceleration * time;
        if (currentSpeed > targetSpeed) {
            currentSpeed = targetSpeed;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

// S7曲线匀加速
void s7CurveUniformAcceleration(double targetSpeed) {
    double currentSpeed = 0.0;
    double time = 0.0;
    
    while (currentSpeed < targetSpeed) {
        currentSpeed += acceleration * time;
        if (currentSpeed > targetSpeed) {
            currentSpeed = targetSpeed;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
    
    while (currentSpeed > 0) {
        currentSpeed -= deceleration * time;
        if (currentSpeed < 0) {
            currentSpeed = 0;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

// S7曲线减加速
void s7CurveDeceleration(double targetSpeed) {
    double currentSpeed = targetSpeed;
    double time = 0.0;
    
    while (currentSpeed > 0) {
        currentSpeed -= deceleration * time;
        if (currentSpeed < 0) {
            currentSpeed = 0;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

// S7曲线匀速
void s7CurveUniformSpeed(double targetSpeed) {
    double currentSpeed = targetSpeed;
    
    std::cout << "Current Speed: " << currentSpeed << std::endl;
}

// S7曲线加减速
void s7CurveAccelerationDeceleration(double targetSpeed) {
    double currentSpeed = 0.0;
    double time = 0.0;
    
    while (currentSpeed < targetSpeed / 2) {
        currentSpeed += acceleration * time;
        if (currentSpeed > targetSpeed / 2) {
            currentSpeed = targetSpeed / 2;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
    
    while (currentSpeed > 0) {
        currentSpeed -= deceleration * time;
        if (currentSpeed < 0) {
            currentSpeed = 0;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

// S7曲线匀减速
void s7CurveUniformDeceleration(double targetSpeed) {
    double currentSpeed = targetSpeed / 2;
    double time = 0.0;
    
    while (currentSpeed > 0) {
        currentSpeed -= deceleration * time;
        if (currentSpeed < 0) {
            currentSpeed = 0;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

// S7曲线减减速
void s7CurveDecelerationDeceleration(double targetSpeed) {
    double currentSpeed = targetSpeed;
    double time = 0.0;
    
    while (currentSpeed > 0) {
        currentSpeed -= deceleration * time;
        if (currentSpeed < 0) {
            currentSpeed = 0;
        }
        
        std::cout << "Current Speed: " << currentSpeed << std::endl;
        
        time += 0.1;
    }
}

int main() {
    double targetSpeed = maxSpeed; // 设置目标速度
    
    // S7曲线加加速
    s7CurveAcceleration(targetSpeed);
    
    // S7曲线匀加速
    s7CurveUniformAcceleration(targetSpeed);
    
    // S7曲线减加速
    s7CurveDeceleration(targetSpeed);
    
    // S7曲线匀速
    s7CurveUniformSpeed(targetSpeed);
    
    // S7曲线加减速
    s7CurveAccelerationDeceleration(targetSpeed);
    
    // S7曲线匀减速
    s7CurveUniformDeceleration(targetSpeed);
    
    // S7曲线减减速
    s7CurveDecelerationDeceleration(targetSpeed);
    
    return 0;
}

改进后的

#include <iostream>
#include <cmath>
#include <windows.h>

// 定义电机参数
const double maxSpeed = 30.0; // 最大速度
const double acceleration = 2.0; // 加速度
const double deceleration = 3.0; // 减速度

//uint16_t process = 0;

// S7曲线加加速
double s7CurveAcceleration(double targetSpeed) {
    static double currentSpeed = 0.0;
    static double time = 0.0;
    static double current_acceleration = 0;

    currentSpeed = currentSpeed + current_acceleration * time;
    current_acceleration += 0.1;
    if(current_acceleration > acceleration)
    {
        return currentSpeed;
    }

    if (currentSpeed > targetSpeed) {
        currentSpeed = targetSpeed;
    }

    std::cout << "S7曲线加加速1 Current Speed: " << currentSpeed << " current_acceleration:" <<current_acceleration << std::endl;

    time += 0.1;
    return currentSpeed;
}

// S7曲线匀加速
double s7CurveUniformAcceleration(double currentSpeed, double targetSpeed) {

    //double currentSpeed = 0.0;
    static double time = 0.0;

    currentSpeed += acceleration * time;
    if (currentSpeed > targetSpeed) {
        currentSpeed = targetSpeed;
    }

    std::cout << "S7曲线匀加速1 Current Speed: " << currentSpeed << std::endl;

    time += 0.1;

    return currentSpeed;
}

// S7曲线减加速 Vt=v0+at
double s7CurveDeceleration(double currentSpeed, double targetSpeed) {
    //double currentSpeed = targetSpeed;
    static double time = 1;
    static double acceleration_ = acceleration;

    currentSpeed += acceleration_ * time;
    acceleration_ -= 0.5;
    if (acceleration_ < 0) {
        acceleration_ = 0;
        std::cout << "S7曲线减加速 Current Speed: " << currentSpeed << " deceleration_:" << acceleration_ << std::endl;
        return currentSpeed;
    }

    if (currentSpeed > targetSpeed) {
        currentSpeed = targetSpeed;
    }

    std::cout << "S7曲线减加速 Current Speed: " << currentSpeed << std::endl;

    time -= 0.1;

    return currentSpeed;
}

// S7曲线匀速
double s7CurveUniformSpeed(double targetSpeed)
{
    double currentSpeed = targetSpeed;

    std::cout << "S7曲线匀速 Current Speed: " << currentSpeed << std::endl;

    return currentSpeed;
}

// S7曲线加减速
double s7CurveAccelerationDeceleration(double currentSpeed, double targetSpeed) {
    //double currentSpeed = 0.0;
    static double time = 0.0;
    static double current_deceleration = 0;

    currentSpeed = currentSpeed + current_deceleration * time;
    if(currentSpeed < -targetSpeed)
    {
        currentSpeed = -targetSpeed;
        return currentSpeed;
    }

    current_deceleration -= 0.5;
    if(current_deceleration > -deceleration)
    {
        current_deceleration = -deceleration;
        std::cout << "S7曲线加减速1 Current Speed: " << currentSpeed << " current_deceleration:" <<current_deceleration << std::endl;
        return currentSpeed;
    }

    std::cout << "S7曲线加减速1 Current Speed: " << currentSpeed << std::endl;

    time += 0.1;

    return currentSpeed;
}

// S7曲线匀减速
double s7CurveUniformDeceleration(double currentSpeed, double targetSpeed) {
    //double currentSpeed = targetSpeed / 2;
    static double time = 1;

    currentSpeed = currentSpeed - deceleration * time;
    if (currentSpeed < -targetSpeed) {
        currentSpeed = -targetSpeed;
        return currentSpeed;
    }

    std::cout << "S7曲线匀减速 Current Speed: " << currentSpeed << std::endl;
    //time += 0.1;

    return currentSpeed;
}

// S7曲线减减速
double s7CurveDecelerationDeceleration(double currentSpeed, double targetSpeed) {
    //double currentSpeed = targetSpeed;
    static double time = 1;
    static double deceleration_ = -deceleration;

    currentSpeed = currentSpeed + deceleration_ * time;
    deceleration_ += 0.5;

    if (deceleration_ > deceleration) {
        deceleration_ = deceleration;
        std::cout << "S7曲线减减速 Current Speed: " << currentSpeed << "deceleration_:" << deceleration_ << std::endl;
    // return currentSpeed;
    }

    if (currentSpeed < -targetSpeed) {
        currentSpeed = -targetSpeed;
        return currentSpeed;
    }else if (currentSpeed > 0) {
        currentSpeed = 0;
    }

    std::cout << "S7曲线减减速 Current Speed: " << currentSpeed << "deceleration_:" << deceleration_ << std::endl;
    //printf("S7曲线减减速 Current Speed:%f\n", currentSpeed);
    //time += 0.1;

    return currentSpeed;
}

int main()
{
    double targetSpeed = maxSpeed; // 设置目标速度
    double nowSpeed = 0.0;
    double process = 0.0;
    while(1)
    {
        Sleep(33);
        process += 0.1;

        if(process > 10)
        {
            printf("break line:%d process:%f\n", __LINE__, process);
            break;
        }
        printf("line:%d process:%f\n", __LINE__, process);

        if(process >= 0 && process <= 2)
        {
            // S7曲线加加速
            nowSpeed = s7CurveAcceleration(targetSpeed);
        }
        else if(process > 2 && process <= 3)
        {
            // S7曲线匀加速
            nowSpeed = s7CurveUniformAcceleration(nowSpeed, targetSpeed);
        }
        else if(process > 3 && process <= 4)
        {
            //printf("line:%d process > 3 && process <= 4 process:%f\n", __LINE__, process);
            // S7曲线减加速
            nowSpeed =  s7CurveDeceleration(nowSpeed, targetSpeed);
        }
        else if(process > 4 && process <= 6)
        {
            // S7曲线匀速
            nowSpeed = s7CurveUniformSpeed(nowSpeed);
        }
        else if(process > 6 && process <= 7)
        {
            // S7曲线加减速
            nowSpeed =s7CurveAccelerationDeceleration(nowSpeed, targetSpeed);
        }
        else if(process > 7 && process <= 8)
        {
            // S7曲线匀减速
            nowSpeed = s7CurveUniformDeceleration(nowSpeed, targetSpeed);
        }
        else if(process > 8 && process <= 10)
        {
            // S7曲线减减速
            nowSpeed = s7CurveDecelerationDeceleration(nowSpeed, targetSpeed);
        }

//        // S7曲线加加速
//        s7CurveAcceleration(targetSpeed);

//        // S7曲线匀加速
//        s7CurveUniformAcceleration(targetSpeed);

//        // S7曲线减加速
//        s7CurveDeceleration(targetSpeed);

//        // S7曲线匀速
//        s7CurveUniformSpeed(targetSpeed);

//        // S7曲线加减速
//        s7CurveAccelerationDeceleration(targetSpeed);

//        // S7曲线匀减速
//        s7CurveUniformDeceleration(targetSpeed);

//        // S7曲线减减速
//        s7CurveDecelerationDeceleration(targetSpeed);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

静思心远

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

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

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

打赏作者

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

抵扣说明:

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

余额充值