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

使用例

#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:
Tue Sep 21 06:16:58 2021 +0000
Revision:
0:0a9ce35078a3
Child:
2:d88ff6dda390
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamohamo 0:0a9ce35078a3 1 #include "core.hpp"
hamohamo 0:0a9ce35078a3 2
hamohamo 0:0a9ce35078a3 3 Core::Core(Robot* robot,int mode,double dt):rbt(robot),mode(mode),dt(dt),Mots(4),Encs(8),PIDs(5){}
hamohamo 0:0a9ce35078a3 4 void Core::addMOT(PinName plus,PinName minus,int period,int id){Mots.at(id) = new Motor(plus,minus,period,id);}
hamohamo 0:0a9ce35078a3 5 void Core::addENC(PinName plus,PinName minus,int resolution,int mode,int id){Encs.at(id) = new Encoder(plus,minus,resolution,mode,id);}
hamohamo 0:0a9ce35078a3 6 void Core::addPID(double Kp,double Ki,double Kd,int id){PIDs.at(id) = new PID(Kp,Ki,Kd,id);}
hamohamo 0:0a9ce35078a3 7 void Core::setLIM(double MOT_max,double MOT_min,double PID_max,double PID_min){
hamohamo 0:0a9ce35078a3 8 Mots[rbt->RF]->setLimit(MOT_max,MOT_min);
hamohamo 0:0a9ce35078a3 9 Mots[rbt->RB]->setLimit(MOT_max,MOT_min);
hamohamo 0:0a9ce35078a3 10 Mots[rbt->LB]->setLimit(MOT_max,MOT_min);
hamohamo 0:0a9ce35078a3 11 Mots[rbt->LF]->setLimit(MOT_max,MOT_min);
hamohamo 0:0a9ce35078a3 12 PIDs[rbt->RF]->setLimit(PID_max,PID_min);
hamohamo 0:0a9ce35078a3 13 PIDs[rbt->RB]->setLimit(PID_max,PID_min);
hamohamo 0:0a9ce35078a3 14 PIDs[rbt->LB]->setLimit(PID_max,PID_min);
hamohamo 0:0a9ce35078a3 15 PIDs[rbt->LF]->setLimit(PID_max,PID_min);
hamohamo 0:0a9ce35078a3 16 }
hamohamo 0:0a9ce35078a3 17 void Core::START(){timer.start();}
hamohamo 0:0a9ce35078a3 18 bool Core::LOOP(){
hamohamo 0:0a9ce35078a3 19 bool ret = true;
hamohamo 0:0a9ce35078a3 20 long long int temp;
hamohamo 0:0a9ce35078a3 21 while(temp <= dt*1000.0*1000.0){
hamohamo 0:0a9ce35078a3 22 temp = timer.read_us()-t;
hamohamo 0:0a9ce35078a3 23 if(temp < 0.0){
hamohamo 0:0a9ce35078a3 24 ret = false;
hamohamo 0:0a9ce35078a3 25 printf("WARN!:Out of cycle:%lld(us)\n",temp);
hamohamo 0:0a9ce35078a3 26 }
hamohamo 0:0a9ce35078a3 27 }
hamohamo 0:0a9ce35078a3 28 t = timer.read_us();
hamohamo 0:0a9ce35078a3 29 return ret;
hamohamo 0:0a9ce35078a3 30 }
hamohamo 0:0a9ce35078a3 31 void Core::WAIT(double wt){while(timer.read_us()-t <= wt*1000.0*1000.0);}
hamohamo 0:0a9ce35078a3 32 void Core::setVelocity(double Vx,double Vy,double Vw){
hamohamo 0:0a9ce35078a3 33 double w1,w2,w3,w4;
hamohamo 0:0a9ce35078a3 34 double k = sqrt(2.0)/2.0;
hamohamo 0:0a9ce35078a3 35 switch(mode){
hamohamo 0:0a9ce35078a3 36 case OMNI4:
hamohamo 0:0a9ce35078a3 37 w1 = -Vx*k*(sin(pos.theta)+cos(pos.theta)) - Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw;
hamohamo 0:0a9ce35078a3 38 w2 = -Vx*k*(sin(pos.theta)-cos(pos.theta)) + Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw;
hamohamo 0:0a9ce35078a3 39 w3 = Vx*k*(sin(pos.theta)+cos(pos.theta)) + Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw;
hamohamo 0:0a9ce35078a3 40 w4 = Vx*k*(sin(pos.theta)-cos(pos.theta)) - Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw;
hamohamo 0:0a9ce35078a3 41 break;
hamohamo 0:0a9ce35078a3 42 case OMNI3:
hamohamo 0:0a9ce35078a3 43 w1 = 0.0;
hamohamo 0:0a9ce35078a3 44 w2 = 0.0;
hamohamo 0:0a9ce35078a3 45 w3 = 0.0;
hamohamo 0:0a9ce35078a3 46 w4 = 0.0;
hamohamo 0:0a9ce35078a3 47 break;
hamohamo 0:0a9ce35078a3 48 case MECANUM:
hamohamo 0:0a9ce35078a3 49 w1 = 0.0;
hamohamo 0:0a9ce35078a3 50 w2 = 0.0;
hamohamo 0:0a9ce35078a3 51 w3 = 0.0;
hamohamo 0:0a9ce35078a3 52 w4 = 0.0;
hamohamo 0:0a9ce35078a3 53 break;
hamohamo 0:0a9ce35078a3 54 }
hamohamo 0:0a9ce35078a3 55 Encs[rbt->RF]->Update(dt);
hamohamo 0:0a9ce35078a3 56 Encs[rbt->RB]->Update(dt);
hamohamo 0:0a9ce35078a3 57 Encs[rbt->LB]->Update(dt);
hamohamo 0:0a9ce35078a3 58 Encs[rbt->LF]->Update(dt);
hamohamo 0:0a9ce35078a3 59 PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 60 PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 61 PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 62 PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 63 }
hamohamo 0:0a9ce35078a3 64 Position Core::getStatus(){
hamohamo 0:0a9ce35078a3 65 double vx,vy,vw;
hamohamo 0:0a9ce35078a3 66 double vX,vY;
hamohamo 0:0a9ce35078a3 67 double Rw1,Rw2,Rw3,Rw4;
hamohamo 0:0a9ce35078a3 68 Encs[rbt->F]->Update(dt);
hamohamo 0:0a9ce35078a3 69 Encs[rbt->R]->Update(dt);
hamohamo 0:0a9ce35078a3 70 Encs[rbt->B]->Update(dt);
hamohamo 0:0a9ce35078a3 71 Encs[rbt->L]->Update(dt);
hamohamo 0:0a9ce35078a3 72 Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR);
hamohamo 0:0a9ce35078a3 73 Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR);
hamohamo 0:0a9ce35078a3 74 Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR);
hamohamo 0:0a9ce35078a3 75 Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR);
hamohamo 0:0a9ce35078a3 76 vx = (-1*Rw1 + Rw3)/2.0;
hamohamo 0:0a9ce35078a3 77 vy = (Rw2 + -1*Rw4)/2.0;
hamohamo 0:0a9ce35078a3 78 vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0;
hamohamo 0:0a9ce35078a3 79 vX = cos(pos.theta)*vx - sin(pos.theta)*vy;
hamohamo 0:0a9ce35078a3 80 vY = sin(pos.theta)*vx + cos(pos.theta)*vy;
hamohamo 0:0a9ce35078a3 81 pos.x += (vel.x+vX)*dt/2.0;
hamohamo 0:0a9ce35078a3 82 pos.y += (vel.y+vY)*dt/2.0;
hamohamo 0:0a9ce35078a3 83 pos.theta += (vel.theta+vw)*dt/2.0;
hamohamo 0:0a9ce35078a3 84 vel.x = vX;
hamohamo 0:0a9ce35078a3 85 vel.y = vY;
hamohamo 0:0a9ce35078a3 86 vel.theta = vw;
hamohamo 0:0a9ce35078a3 87 return pos;
hamohamo 0:0a9ce35078a3 88 }