足回り動かすためのライブラリ

使用例

#include "scrp_slave.hpp"
#include "core.hpp"

#include "mbed.h"

ScrpSlave sendpwm(PC_12,PD_2 ,PH_1 ,SERIAL_TX,SERIAL_RX,0x0807f800);
Robot AKASHIKOSEN(50.8,25.4,322.5,259.75);
Core RBT(&AKASHIKOSEN,OMNI4,0.02);

int main(){

/*--------------SETUP--------------*/
AKASHIKOSEN.setCWID(0,1,2,3);
AKASHIKOSEN.setSWID(4,5,6,7);
    
RBT.addENC(PC_4,PA_13,512,4,0);
RBT.addENC(PA_14,PA_15,512,4,1);
RBT.addENC(PC_2,PC_3,512,4,2);
RBT.addENC(PC_10,PC_11,512,4,3);
RBT.addENC(PA_7,PA_6,512,4,4);
RBT.addENC(PA_9,PA_8,512,4,5);
RBT.addENC(PC_1,PC_0,512,4,6);
RBT.addENC(PC_5,PA_12,512,4,7);

RBT.START();
/*--------------LOOP--------------*/
Position pos;
while(true){
    pos = RBT.getStatus();
    printf("x:%lf,y:%lf,theta:%lf\n",pos.x,pos.y,pos.theta);
    RBT.LOOP();
}
}
Committer:
hamohamo
Date:
Fri Oct 29 09:20:31 2021 +0000
Revision:
11:80800fd9f4af
Parent:
9:489046c1e624
asd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamohamo 0:0a9ce35078a3 1 #include "encoder.hpp"
hamohamo 0:0a9ce35078a3 2
hamohamo 0:0a9ce35078a3 3 Encoder::Encoder(PinName pinA,PinName pinB,int resolution,int mode,int id):resolution(resolution),mode(mode),ID(id){
hamohamo 0:0a9ce35078a3 4 pin_A = new InterruptIn(pinA,PullUp);
hamohamo 0:0a9ce35078a3 5 pin_B = new InterruptIn(pinB,PullUp);
hamohamo 9:489046c1e624 6 pulse = 0;
hamohamo 9:489046c1e624 7 theta[0] = 0.0;
hamohamo 9:489046c1e624 8 theta[1] = 0.0;
hamohamo 9:489046c1e624 9 omega[0] = 0.0;
hamohamo 9:489046c1e624 10 omega[1] = 0.0;
hamohamo 9:489046c1e624 11 alpha[0] = 0.0;
hamohamo 9:489046c1e624 12 alpha[1] = 0.0;
hamohamo 9:489046c1e624 13 jerk = 0.0;
hamohamo 0:0a9ce35078a3 14 init();
hamohamo 0:0a9ce35078a3 15 }
hamohamo 0:0a9ce35078a3 16 Encoder::~Encoder(){
hamohamo 0:0a9ce35078a3 17 pin_B->disable_irq();
hamohamo 0:0a9ce35078a3 18 pin_B->disable_irq();
hamohamo 0:0a9ce35078a3 19 delete pin_A;
hamohamo 0:0a9ce35078a3 20 delete pin_B;
hamohamo 0:0a9ce35078a3 21 }
hamohamo 0:0a9ce35078a3 22
hamohamo 0:0a9ce35078a3 23 void Encoder::Update(double dt){
hamohamo 0:0a9ce35078a3 24 theta[1] = 2.0*M_PI * pulse / resolution / mode;
hamohamo 0:0a9ce35078a3 25 omega[1] = (theta[1]-theta[0])/dt;
hamohamo 0:0a9ce35078a3 26 alpha[1] = (omega[1]-omega[0])/dt;
hamohamo 0:0a9ce35078a3 27 jerk = (alpha[1]-alpha[0])/dt;
hamohamo 0:0a9ce35078a3 28 theta[0] = theta[1];
hamohamo 0:0a9ce35078a3 29 omega[0] = omega[1];
hamohamo 0:0a9ce35078a3 30 alpha[0] = alpha[1];
hamohamo 0:0a9ce35078a3 31 }
hamohamo 0:0a9ce35078a3 32
hamohamo 0:0a9ce35078a3 33 double Encoder::get_Theta() {return theta[1];}
hamohamo 0:0a9ce35078a3 34 double Encoder::get_Omega() {return omega[1];}
hamohamo 0:0a9ce35078a3 35 double Encoder::get_Alpha() {return alpha[1];}
hamohamo 0:0a9ce35078a3 36 double Encoder::get_Jerk() {return jerk;}
hamohamo 0:0a9ce35078a3 37 double Encoder::get_ID() {return ID;}
hamohamo 0:0a9ce35078a3 38
hamohamo 0:0a9ce35078a3 39 void Encoder::init(){
hamohamo 0:0a9ce35078a3 40 pulse = 0;
hamohamo 0:0a9ce35078a3 41 if(mode <= 0 or 4 < mode) mode = 4;
hamohamo 0:0a9ce35078a3 42 if(1 <= mode) pin_A->rise(callback(this,&Encoder::riseA));
hamohamo 0:0a9ce35078a3 43 if(2 <= mode) pin_A->fall(callback(this,&Encoder::fallA));
hamohamo 0:0a9ce35078a3 44 if(3 <= mode) pin_B->rise(callback(this,&Encoder::riseB));
hamohamo 0:0a9ce35078a3 45 if(4 <= mode) pin_B->fall(callback(this,&Encoder::fallB));
hamohamo 0:0a9ce35078a3 46 }
hamohamo 0:0a9ce35078a3 47
hamohamo 0:0a9ce35078a3 48 void Encoder::riseA(void){pin_B->read() ? pulse-- : pulse++;}
hamohamo 0:0a9ce35078a3 49 void Encoder::riseB(void){pin_A->read() ? pulse++ : pulse--;}
hamohamo 0:0a9ce35078a3 50 void Encoder::fallA(void){pin_B->read() ? pulse++ : pulse--;}
hamohamo 0:0a9ce35078a3 51 void Encoder::fallB(void){pin_A->read() ? pulse-- : pulse++;}