Octopus!!

Dependencies:   2017NHKpin_config FEP HMC6352 PID QEI R1307 ikarashiMDC omni_wheel

Fork of KANIv3 by NagaokaRoboticsClub_mbedTeam

bot/bot.cpp

Committer:
takeuchi
Date:
2017-10-01
Branch:
develop1
Revision:
23:797d25f3df5e
Parent:
19:34da005ea4ea

File content as of revision 23:797d25f3df5e:

#include "bot.h"

Bot::Bot() :
    pidc(),
    pad(XBee2TX, XBee2RX, ADDR),
    RS485(MDTX, MDRX, 38400),
    RS485Controller(PWM1),
    powerSwitch(MDstop),
    quadOmni(&RS485Controller, &RS485),
    slider(&RS485Controller, &RS485),
    armMotor({
        ikarashiMDC({&RS485Controller, 1, 1, SM, &RS485}),
        ikarashiMDC({&RS485Controller, 1, 2, SM, &RS485}),
        ikarashiMDC({&RS485Controller, 1, 3, SM, &RS485})
    }),
    receiveSuccessed(0),
    led({DebugLED3, DebugLED4, DebugLED5}),
    debugSerial(USBTX, USBRX, 115200)
{
    for(int i = 0; i < 3; i++) {
        armMotor[i].braking = true;
    }

    quadOmni.moveXY(0, 0, 0);

    powerSwitch = true;
    for(int i = 0; i < 3; i++) {
        led[i] = true;
        wait(0.1);
        led[i] = false;
    }
}

void Bot::confirmAll()
{
    receiveSuccessed = pad.receiveState();
    if(!pad.getButton2(3) && !pad.getButton2(0)) {
        powerSwitch = 0;
    }
    led[0] = receiveSuccessed;
    if(!receiveSuccessed) {
        quadOmni.moveXY(0, 0, 0);
        slider.slide(0);
        for(int i = 0; i < 3; i++) {
            armMotor[i].setSpeed(0);
        }
    }
}

void Bot::controllDrive()
{
    static int mode = 1;
    if(receiveSuccessed) {
        debugSerial.printf("%d\n\r", pidc.getCurrentDegree());

        if(!pad.getButton2(4)) {
            mode = 1;
        }

        if(!pad.getButton2(5)) {
            mode = 2;
        }

        if(mode == 1) {
            pidc.setPoint(0.0);
            pidc.confirm();

            quadOmni.moveXY(
                pad.getStick(0),
                -pad.getStick(1),
                -pidc.getCalculationResult()
            );
        }
        if(mode == 2) {
            pidc.setPoint(90.0);
            pidc.confirm();

            quadOmni.moveXY(
                -pad.getStick(1),
                -pad.getStick(0),
                -pidc.getCalculationResult()
            );
        }
    } else {
        quadOmni.moveXY(0, 0, 0);
    }
}

void Bot::controllMech()
{
    if(receiveSuccessed) {
        if(!pad.getButton1(6)) {
            armMotor[SWORD].setSpeed(0.6);
        }
        if(pad.getButton1(6)) armMotor[SWORD].setSpeed(0.0);

        slider.slide(pad.getStick(3));

        if(!pad.getButton2(1)) {
            armMotor[DESTROY].setSpeed(-1.0);
        } else {
            armMotor[DESTROY].setSpeed(0.0);
        }
   } else {
       slider.slide(0);
       for(int i = 0; i < 3; i++) {
           armMotor[i].setSpeed(0);
       }
   }
}


void Bot::calibrate()
{
    if(receiveSuccessed &&
         !pad.getButton2(0) &&
         !pad.getButton2(1)
     ) {
        t.start();
        t.reset();
        pidc.calibration(HMC6352_ENTER_CALIB);
        while(t.read() < 5.0) {
            quadOmni.moveXY(0, 0, 0.4);
            slider.slide(0);
            for(int i = 0; i < 3; i++) {
                armMotor[i].setSpeed(0);
            }
        }
        t.stop();
        quadOmni.moveXY(0, 0, 0);
        pidc.calibration(HMC6352_EXIT_CALIB);
    }
}