Ain x & y

Dependents:   aControlUnit

Joystick.cpp

Committer:
pkolar1
Date:
2018-12-19
Revision:
4:2e370ac02328
Parent:
3:a3ec28483a72

File content as of revision 4:2e370ac02328:

#include "mbed.h"
#include "Joystick.h"

Joystick::Joystick(PinName xPin, PinName yPin) : xi(xPin), yi(yPin)
{
    init();
    t.attach(this,&Joystick::floattoint,0.04);
}

void Joystick::init()
{
    dirr = 0;     // initialized to stay still
    aimm = 0;     // servo in mid aim
}

void Joystick::floattoint()
{
    x = (int)(100*xi.read());
    y = (int)(100*yi.read()); // float to int
}

int Joystick::DirectionDC()
{
    if(x<=80 && x>=50 && y<=83 && y>=53) {        //still
        dirr = 0;
    } else if(x<50 &&x>=0 && y<53 && y>=0) {      //forward
        dirr = 4;
    } else if(x<=100 && x>80 && y<=100 && y>83) { //backward
        dirr = 2;
    }
    return dirr;
}

int Joystick::Servo()
{
    if(dirr == 0) {                     //still
        aimm = 0;
    } else if(dirr == 4) {
        if(abs(x - y) < 20) aimm = 0;
        else if(x < y) {                //left
            aimm = 8;
        } else if(x > y) {              //right
            aimm = 1;
        }
    } else if(dirr == 2) {              //backward
        if(x == 100 && y == 100) aimm = 0;
        else if(x < y) {                //left
            aimm = 8;
        } else if(x > y) {              //right
            aimm = 1;
        }
    }
    return aimm;
}