sample

Dependents:   wheel_test6

sample02.cpp

Committer:
piroro4560
Date:
2019-07-11
Revision:
3:8d1778dbf580
Parent:
2:b51bb221438a
Child:
4:3eefc20c8312
Child:
6:1e1adccfb69a

File content as of revision 3:8d1778dbf580:

#include "sample02.h"

Sample::Sample(double accdis_, double decdis_, double maxspeed_, double s_vector_)
{
    accdis   = accdis_;
    decdis   = decdis_;
    maxspeed = maxspeed_;
    s_vector = s_vector_;
}

void Sample::target_xy(int targetx_, int targety_, int startx_, int starty_)
{
    targetDis = hypot((float)(startx_-targetx_), (float)(starty_-targety_));
    if (targetDis > accdis+decdis) {
        startx    = startx_;
        starty    = starty_;
        targetx   = targetx_;
        targety   = targety_;
        accsec    = 2.0 / maxspeed * accdis;
        decsec    = 2.0 / maxspeed * decdis;
        middledis = targetDis - accdis - decdis;
    }
}

void Sample::calculate(int now_x, int now_y)
{
    nowx = now_x;
    nowy = now_y;
    nowDis = hypot((startx-nowx),(starty-nowy));
    targetRad = atan2((double)(targety-nowy), (double)(targetx-nowx));
    /***/
    if (nowDis < accdis) {
        
        counter = sqrt(2.0*accsec/maxspeed*nowDis);
        vector = s_vector + (-1.0 * sin((2.0*PI/accsec)*counter) + (2.0*PI/accsec)*counter) / ((2.0*PI)/(maxspeed-s_vector));
        
    } else if (nowDis >= accdis && nowDis < accdis+middledis) {
        vector = maxspeed;
    } else if (nowDis > (accdis+middledis) && nowDis <= targetDis) {
        
        counter = sqrt(2.0*decsec/maxspeed*fabs(targetDis-nowDis));
        vector = (-1.0 * sin((2.0*PI/decsec)*counter) + (2.0*PI/decsec)*counter) / (2.0*PI/maxspeed)*0.9;
    }
    /***/
}

double Sample::getvalue_x()
{
    return vector * cos(targetRad);
}

double Sample::getvalue_y()
{
    return vector * sin(targetRad);
}