yei

Dependencies:   interface mbed enc_1multi calPID motorout KondoServoLibrary

Fork of cat18_operate by Catch the GIANT Caplico!

interrupt/interrupt.cpp

Committer:
shimizuta
Date:
2018-08-17
Revision:
19:48c3af917932
Parent:
18:05f5a3323bda
Child:
20:13934809e117

File content as of revision 19:48c3af917932:

#include "interrupt.h"
#include "mbed.h"
#include "go.h"
#include "check.h"
#include "coordinate.h"
#include "pinnames.h"
#include "cat18_can_info.h"
#include "debug.h"
#include "workposition.h"
#define RIGHT//右用ならRIGHT,左用ならLEFT

Ticker ticker;
CAN can(pin_can_rd,pin_can_td);
int counterpart_is_in_common;
char can_pub_data[kCanlength_rightleft];

const double kTicker_s = 0.001;
const int kCanCount = kCanTicker_s / kTicker_s;
const int kPIDCount = kPIDTicker_s / kTicker_s;
const int kServoCalCount = kServoCalTicker_s / kTicker_s;
const int kServoMoveCount = kServoMoveTicker_s / kTicker_s;
const int kCountReset=kPIDCount*kServoCalCount*kCanCount*kServoMoveCount;

void TimerInterrupt();
void CanRead();
void CanSend();

void InterruptSetup()
{
    can.frequency(kCanFrequency);
    ticker.attach(TimerInterrupt,kTicker_s);
}
void TimerInterrupt()
{
    static int count = 0;
    if(count % kPIDCount == 0) PIDInterrupt();
    if(count % kServoCalCount==0){
        ServoCalInterrupt();
    }
    //if(count % kServoMoveCount==0){
    //    ServoMoveInterrupt();
    //}
    /*
    if(count % kCanCount==0){
        CanRead();
        CanSend();
    }
    */
    if(count == kCountReset) count=0;
    count++;
}
int GetCounterpartIsInCommon()
{
    if(shootingbox[9].is_exist == 0) counterpart_is_in_common = 1;
    else counterpart_is_in_common = 0;
    return counterpart_is_in_common;
}

void CanRead()
{
    CANMessage msg;
    if(!can.read(msg)) {
        return;
    }
    switch (msg.id) {
#ifdef RIGHT
        case CANID_LEFT :
#elif defined LEFT
        case CANID_RIGHT :
#endif
            counterpart_is_in_common = msg.data[0];
            break;

    }
}
void CanSend()
{
    static int state = 1;
    switch(state) {
#ifdef RIGHT
        case CANID_RIGHT :
#elif defined LEFT
        case CANID_LEFT:
#endif
        {
            double now_position_mm[3] = {GetNowTipLocateX(),GetNowTipLocateY(),GetNowTipLocateZ()};
            if(CheckBanArea(0, now_position_mm[0], now_position_mm) == 0) can_pub_data[0] = 1;
            else can_pub_data[0] = 0;
            CANMessage msg(state,can_pub_data,kCanlength_rightleft);
            can.write(msg);
            break;
        }
    }
    state ++;
    if(state >= CANID_FINISH) {
        state = 1;
    }
}