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

使用例

#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:
Thu Oct 28 10:58:56 2021 +0000
Revision:
10:f434b6848059
Parent:
9:489046c1e624
Child:
11:80800fd9f4af
asd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamohamo 0:0a9ce35078a3 1 #include "core.hpp"
hamohamo 0:0a9ce35078a3 2
hamohamo 9:489046c1e624 3 Core::Core(Robot* robot,int mode,double dt):rbt(robot),mode(mode),dt(dt),Mots(5),Encs(9),PIDs(6){
hamohamo 9:489046c1e624 4 pos.x = 0.0;
hamohamo 9:489046c1e624 5 pos.y = 0.0;
hamohamo 9:489046c1e624 6 pos.theta = 0.0;
hamohamo 9:489046c1e624 7 vel.x = 0.0;
hamohamo 9:489046c1e624 8 vel.y = 0.0;
hamohamo 9:489046c1e624 9 vel.theta = 0.0;}
hamohamo 2:d88ff6dda390 10 Motor* Core::addMOT(PinName plus,PinName minus,int period,int id){
hamohamo 2:d88ff6dda390 11 Mots.at(id) = new Motor(plus,minus,period,id);
hamohamo 2:d88ff6dda390 12 return Mots.at(id);
hamohamo 2:d88ff6dda390 13 }
hamohamo 0:0a9ce35078a3 14 void Core::addENC(PinName plus,PinName minus,int resolution,int mode,int id){Encs.at(id) = new Encoder(plus,minus,resolution,mode,id);}
hamohamo 2:d88ff6dda390 15 PID* Core::addPID(double Kp,double Ki,double Kd,int id){
hamohamo 2:d88ff6dda390 16 PIDs.at(id) = new PID(Kp,Ki,Kd,id);
hamohamo 2:d88ff6dda390 17 return PIDs.at(id);
hamohamo 0:0a9ce35078a3 18 }
hamohamo 10:f434b6848059 19 IMC* Core::addIMC(double K,double T,int id){
hamohamo 10:f434b6848059 20 IMCs.at(id) = new IMC(K,T,id);
hamohamo 10:f434b6848059 21 return IMCs.at(id);
hamohamo 10:f434b6848059 22 }
hamohamo 2:d88ff6dda390 23 void Core::setPWM(double pwm,int id){Mots[id]->setPWM(pwm);}
hamohamo 2:d88ff6dda390 24
hamohamo 0:0a9ce35078a3 25 void Core::START(){timer.start();}
hamohamo 0:0a9ce35078a3 26 bool Core::LOOP(){
hamohamo 0:0a9ce35078a3 27 bool ret = true;
hamohamo 0:0a9ce35078a3 28 long long int temp;
hamohamo 0:0a9ce35078a3 29 while(temp <= dt*1000.0*1000.0){
hamohamo 0:0a9ce35078a3 30 temp = timer.read_us()-t;
hamohamo 0:0a9ce35078a3 31 if(temp < 0.0){
hamohamo 0:0a9ce35078a3 32 ret = false;
hamohamo 0:0a9ce35078a3 33 printf("WARN!:Out of cycle:%lld(us)\n",temp);
hamohamo 0:0a9ce35078a3 34 }
hamohamo 0:0a9ce35078a3 35 }
hamohamo 0:0a9ce35078a3 36 t = timer.read_us();
hamohamo 0:0a9ce35078a3 37 return ret;
hamohamo 0:0a9ce35078a3 38 }
hamohamo 0:0a9ce35078a3 39 void Core::WAIT(double wt){while(timer.read_us()-t <= wt*1000.0*1000.0);}
hamohamo 2:d88ff6dda390 40 void Core::setPosition(double x,double y,double theta){
hamohamo 2:d88ff6dda390 41 pos.x = x;
hamohamo 2:d88ff6dda390 42 pos.y = y;
hamohamo 2:d88ff6dda390 43 pos.theta = theta;
hamohamo 2:d88ff6dda390 44 }
hamohamo 0:0a9ce35078a3 45 void Core::setVelocity(double Vx,double Vy,double Vw){
hamohamo 0:0a9ce35078a3 46 double w1,w2,w3,w4;
hamohamo 7:c0b6afbccd97 47 double vx,vy;
hamohamo 7:c0b6afbccd97 48 vx = cos(pos.theta-M_PI/4.0)*Vx + sin(pos.theta-M_PI/4.0)*Vy;
hamohamo 7:c0b6afbccd97 49 vy = - sin(pos.theta-M_PI/4.0)*Vx + cos(pos.theta-M_PI/4.0)*Vy;
hamohamo 0:0a9ce35078a3 50 switch(mode){
hamohamo 0:0a9ce35078a3 51 case OMNI4:
hamohamo 7:c0b6afbccd97 52 w1 = -vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 53 w2 = vy + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 54 w3 = vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 55 w4 = -vy + rbt->C2CD*Vw;
hamohamo 0:0a9ce35078a3 56 break;
hamohamo 0:0a9ce35078a3 57 case OMNI3:
hamohamo 0:0a9ce35078a3 58 w1 = 0.0;
hamohamo 0:0a9ce35078a3 59 w2 = 0.0;
hamohamo 0:0a9ce35078a3 60 w3 = 0.0;
hamohamo 0:0a9ce35078a3 61 w4 = 0.0;
hamohamo 0:0a9ce35078a3 62 break;
hamohamo 0:0a9ce35078a3 63 case MECANUM:
hamohamo 0:0a9ce35078a3 64 w1 = 0.0;
hamohamo 0:0a9ce35078a3 65 w2 = 0.0;
hamohamo 0:0a9ce35078a3 66 w3 = 0.0;
hamohamo 0:0a9ce35078a3 67 w4 = 0.0;
hamohamo 0:0a9ce35078a3 68 break;
hamohamo 8:475c17b23944 69 }/*
hamohamo 0:0a9ce35078a3 70 Encs[rbt->RF]->Update(dt);
hamohamo 0:0a9ce35078a3 71 Encs[rbt->RB]->Update(dt);
hamohamo 0:0a9ce35078a3 72 Encs[rbt->LB]->Update(dt);
hamohamo 8:475c17b23944 73 Encs[rbt->LF]->Update(dt);*/
hamohamo 0:0a9ce35078a3 74 PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 75 PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 76 PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt);
hamohamo 0:0a9ce35078a3 77 PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt);
hamohamo 8:475c17b23944 78
hamohamo 6:87fd489a9801 79 setPWM(PIDs[rbt->RF]->getmv(),rbt->RF);
hamohamo 6:87fd489a9801 80 setPWM(PIDs[rbt->RB]->getmv(),rbt->RB);
hamohamo 6:87fd489a9801 81 setPWM(PIDs[rbt->LB]->getmv(),rbt->LB);
hamohamo 6:87fd489a9801 82 setPWM(PIDs[rbt->LF]->getmv(),rbt->LF);
hamohamo 0:0a9ce35078a3 83 }
hamohamo 6:87fd489a9801 84 Position Core::getStatus(bool onground){
hamohamo 0:0a9ce35078a3 85 double vx,vy,vw;
hamohamo 0:0a9ce35078a3 86 double vX,vY;
hamohamo 0:0a9ce35078a3 87 double Rw1,Rw2,Rw3,Rw4;
hamohamo 8:475c17b23944 88 Encs[rbt->F]->Update(dt);
hamohamo 8:475c17b23944 89 Encs[rbt->R]->Update(dt);
hamohamo 8:475c17b23944 90 Encs[rbt->B]->Update(dt);
hamohamo 8:475c17b23944 91 Encs[rbt->L]->Update(dt);
hamohamo 8:475c17b23944 92 Encs[rbt->RF]->Update(dt);
hamohamo 8:475c17b23944 93 Encs[rbt->RB]->Update(dt);
hamohamo 8:475c17b23944 94 Encs[rbt->LB]->Update(dt);
hamohamo 8:475c17b23944 95 Encs[rbt->LF]->Update(dt);
hamohamo 6:87fd489a9801 96 if(onground){
hamohamo 6:87fd489a9801 97
hamohamo 6:87fd489a9801 98 Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 99 Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 100 Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 101 Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 102 }
hamohamo 6:87fd489a9801 103 else {
hamohamo 6:87fd489a9801 104
hamohamo 6:87fd489a9801 105 Rw1 = Encs[rbt->RF]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 106 Rw2 = Encs[rbt->RB]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 107 Rw3 = Encs[rbt->LB]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 108 Rw4 = Encs[rbt->LF]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 109 }
hamohamo 0:0a9ce35078a3 110 vx = (-1*Rw1 + Rw3)/2.0;
hamohamo 0:0a9ce35078a3 111 vy = (Rw2 + -1*Rw4)/2.0;
hamohamo 0:0a9ce35078a3 112 vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0;
hamohamo 6:87fd489a9801 113 if(onground){
hamohamo 6:87fd489a9801 114 vX = cos(pos.theta)*vx - sin(pos.theta)*vy;
hamohamo 6:87fd489a9801 115 vY = sin(pos.theta)*vx + cos(pos.theta)*vy;
hamohamo 6:87fd489a9801 116 }
hamohamo 6:87fd489a9801 117 else {
hamohamo 7:c0b6afbccd97 118 vX = cos(pos.theta-M_PI/4.0)*vx - sin(pos.theta-M_PI/4.0)*vy;
hamohamo 7:c0b6afbccd97 119 vY = sin(pos.theta-M_PI/4.0)*vx + cos(pos.theta-M_PI/4.0)*vy;
hamohamo 6:87fd489a9801 120 }
hamohamo 2:d88ff6dda390 121
hamohamo 0:0a9ce35078a3 122 pos.x += (vel.x+vX)*dt/2.0;
hamohamo 0:0a9ce35078a3 123 pos.y += (vel.y+vY)*dt/2.0;
hamohamo 0:0a9ce35078a3 124 pos.theta += (vel.theta+vw)*dt/2.0;
hamohamo 0:0a9ce35078a3 125 vel.x = vX;
hamohamo 0:0a9ce35078a3 126 vel.y = vY;
hamohamo 0:0a9ce35078a3 127 vel.theta = vw;
hamohamo 0:0a9ce35078a3 128 return pos;
hamohamo 2:d88ff6dda390 129 }
hamohamo 4:2425cd08e0c2 130 Core::Core(Robot* robot,ScrpSlave* scrp,int mode,double dt):rbt(robot),scrp(scrp),mode(mode),dt(dt),Mots(5),Encs(9),PIDs(6){}
hamohamo 5:8dec5e011b3c 131 void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100.0));}
hamohamo 2:d88ff6dda390 132 void Core::sendVelocity(double Vx,double Vy,double Vw){
hamohamo 2:d88ff6dda390 133 double w1,w2,w3,w4;
hamohamo 7:c0b6afbccd97 134 double vx,vy;
hamohamo 7:c0b6afbccd97 135 vx = cos(pos.theta-M_PI/4.0)*Vx + sin(pos.theta-M_PI/4.0)*Vy;
hamohamo 7:c0b6afbccd97 136 vy = - sin(pos.theta-M_PI/4.0)*Vx + cos(pos.theta-M_PI/4.0)*Vy;
hamohamo 2:d88ff6dda390 137 switch(mode){
hamohamo 2:d88ff6dda390 138 case OMNI4:
hamohamo 7:c0b6afbccd97 139 w1 = -vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 140 w2 = vy + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 141 w3 = vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 142 w4 = -vy + rbt->C2CD*Vw;
hamohamo 2:d88ff6dda390 143 break;
hamohamo 2:d88ff6dda390 144 case OMNI3:
hamohamo 2:d88ff6dda390 145 w1 = 0.0;
hamohamo 2:d88ff6dda390 146 w2 = 0.0;
hamohamo 2:d88ff6dda390 147 w3 = 0.0;
hamohamo 2:d88ff6dda390 148 w4 = 0.0;
hamohamo 2:d88ff6dda390 149 break;
hamohamo 2:d88ff6dda390 150 case MECANUM:
hamohamo 2:d88ff6dda390 151 w1 = 0.0;
hamohamo 2:d88ff6dda390 152 w2 = 0.0;
hamohamo 2:d88ff6dda390 153 w3 = 0.0;
hamohamo 2:d88ff6dda390 154 w4 = 0.0;
hamohamo 2:d88ff6dda390 155 break;
hamohamo 8:475c17b23944 156 }/*
hamohamo 2:d88ff6dda390 157 Encs[rbt->RF]->Update(dt);
hamohamo 2:d88ff6dda390 158 Encs[rbt->RB]->Update(dt);
hamohamo 2:d88ff6dda390 159 Encs[rbt->LB]->Update(dt);
hamohamo 8:475c17b23944 160 Encs[rbt->LF]->Update(dt);*/
hamohamo 10:f434b6848059 161 /*
hamohamo 2:d88ff6dda390 162 PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt);
hamohamo 2:d88ff6dda390 163 PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt);
hamohamo 2:d88ff6dda390 164 PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt);
hamohamo 10:f434b6848059 165 PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt);*/
hamohamo 10:f434b6848059 166
hamohamo 10:f434b6848059 167 IMCs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt);
hamohamo 10:f434b6848059 168 IMCs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt);
hamohamo 10:f434b6848059 169 IMCs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt);
hamohamo 10:f434b6848059 170 IMCs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt);
hamohamo 10:f434b6848059 171
hamohamo 10:f434b6848059 172 printf("%lf,%lf,%lf\n",Encs[1]->get_Omega(),w1/rbt->CWR,IMCs[1]->getmv());
hamohamo 10:f434b6848059 173 /*
hamohamo 2:d88ff6dda390 174 WAIT(0.01);
hamohamo 6:87fd489a9801 175 sendPWM(PIDs[rbt->RF]->getmv(),rbt->RF);
hamohamo 2:d88ff6dda390 176 WAIT(0.0125);
hamohamo 6:87fd489a9801 177 sendPWM(PIDs[rbt->RB]->getmv(),rbt->RB);
hamohamo 2:d88ff6dda390 178 WAIT(0.015);
hamohamo 6:87fd489a9801 179 sendPWM(PIDs[rbt->LB]->getmv(),rbt->LB);
hamohamo 2:d88ff6dda390 180 WAIT(0.0175);
hamohamo 10:f434b6848059 181 sendPWM(PIDs[rbt->LF]->getmv(),rbt->LF);*/
hamohamo 10:f434b6848059 182
hamohamo 10:f434b6848059 183 WAIT(0.01);
hamohamo 10:f434b6848059 184 sendPWM(IMCs[rbt->RF]->getmv(),rbt->RF);
hamohamo 10:f434b6848059 185 WAIT(0.0125);
hamohamo 10:f434b6848059 186 sendPWM(IMCs[rbt->RB]->getmv(),rbt->RB);
hamohamo 10:f434b6848059 187 WAIT(0.015);
hamohamo 10:f434b6848059 188 sendPWM(IMCs[rbt->LB]->getmv(),rbt->LB);
hamohamo 10:f434b6848059 189 WAIT(0.0175);
hamohamo 10:f434b6848059 190 sendPWM(IMCs[rbt->LF]->getmv(),rbt->LF);
hamohamo 0:0a9ce35078a3 191 }