kani

Dependencies:   2017NHKpin_config FEP omni_wheel PID R1307 ikarashiMDC

classDiagram

    \ ̄\                   / ̄/ 
/l     \  \             /  / lヽ  
| ヽ  ヽ   |           |  /  / | 
\ ` ‐ヽ  ヽ  ●        ●         /  / ‐  / 
  \ __ l  |  ||___|| /  l __ / 
     \  \ /      \/ 
      /\|   人__人  |/\    <ズワイガニ  
    //\|             |/\\     
    //\|   ケガニ            |/\\     
    /     . \_____/         \ 

                               ┏┓        ┏━┓┏┓              
     ┏┓         ┏┓┏┓   ┏┓    ┏┓┗┛     ┏┓ ┗┓┃┗┛              
┏┛┗━┓  ┃┃┃┃    ┃┃┏━┛┗┓┏┓┏┛┗━┓┃┃┏┓┏┓┏━━━┓ 
┗┓┏━┛  ┃┃┗┛    ┃┃┗━┓┏┛┗┛┗┓┏┓┃┗┛┗┛┃┃┗━━━┛    
┏┛┃┏━┓┃┗━━┓┃┃┏━┛┗┓      ┏┛┃┃┃        ┃┃              
┃┏┛┗━┛┗━━┓┃┃┃┃┏┓┏┛      ┗━┛┃┃        ┃┃┏┓          
┃┃┏━━┓┏━━┛┃┃┃┃┗┛┃         ┏┛┃        ┃┃┃┗━━┓    
┗┛┗━━┛┗━━━┛┗┛┗━━┛         ┗━┛        ┗┛┗━━━┛  

bot/PIDcontroller/PID_controller.cpp

Committer:
uchitake
Date:
2017-09-06
Revision:
5:16ea97725085
Parent:
3:369d9ee17e84
Child:
6:fe9767a50891

File content as of revision 5:16ea97725085:

#include "PID_controller.h"

void PIDC::updateOutput()
{
    confirm();
}

PIDC::PIDC() :
    PID(KC, TI, TD, INTERVAL),
    HMC6352(HMCsda, HMCscl),
    rawDegree(0),
    offSetDegree(0),
    turnOverNumber(0),
    beforeDegree(0),
    co(0),
    processValue(0),
    initDegree(0)
{
    PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT);
    PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT);
    PID::setBias(BIAS);
    PID::setMode(AUTO_MODE);
    PID::setSetPoint(0.0);

    wait(0.1);
    HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20);
    wait(0.1);
    rawDegree = HMC6352::sample();
    beforeDegree = rawDegree;
    offSetDegree = rawDegree;
    baseAngle = rawDegree;
    initDegree = 0;
    turnOverNumber = 0;
//    this -> attach(this, &PIDC::updateOutput, INTERVAL);
}

PIDC::PIDC(PinName sda, PinName scl, float kc, float ti, float td, float interval) :
    PID(kc, ti, td, interval),
    HMC6352(sda, scl),
    rawDegree(0),
    offSetDegree(0),
    turnOverNumber(0),
    beforeDegree(0),
    co(0),
    processValue(0),
    initDegree(0)
{
    PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT);
    PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT);
    PID::setBias(BIAS);
    PID::setMode(AUTO_MODE);
    PID::setSetPoint(0.0);

    wait(0.1);
    HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20);
    wait(0.1);
    rawDegree = HMC6352::sample();
    beforeDegree = rawDegree;
    offSetDegree = rawDegree;
    offSetDegree2 = rawDegree;
    baseAngle = rawDegree;
    initDegree = 0;
    turnOverNumber = 0;

//    this -> attach(this, &PIDC::updateOutput, INTERVAL);
}


void PIDC::confirm()
{
    rawDegree = HMC6352::sample();
    if(rawDegree - beforeDegree < -1800) turnOverNumber++;
    if(rawDegree - beforeDegree > 1800) turnOverNumber--;
    initDegree = rawDegree - offSetDegree + turnOverNumber * 3600;
    initDegree2 = rawDegree - offSetDegree2 + turnOverNumber*3600;
    beforeDegree = HMC6352::sample();
    PID::setProcessValue(initDegree / 10.0);
    co = PID::compute();
}

float PIDC::getCo() const
{
    return co;
}

int PIDC::getDegree() const
{
    return initDegree;
}

void PIDC::resetOffset()
{
    beforeDegree = HMC6352::sample();
    offSetDegree = HMC6352::sample();
    turnOverNumber = 0;
}

void PIDC::resetOffset2()
{
    beforeDegree = HMC6352::sample();
    offSetDegree2 = HMC6352::sample();
    turnOverNumber = 0;
}

void PIDC::calibration(int mode)
{
    setCalibrationMode(mode);
}