足回り動かすためのライブラリ
使用例
#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(); } }
core.cpp
- Committer:
- hamohamo
- Date:
- 2021-09-21
- Revision:
- 0:0a9ce35078a3
- Child:
- 2:d88ff6dda390
File content as of revision 0:0a9ce35078a3:
#include "core.hpp" Core::Core(Robot* robot,int mode,double dt):rbt(robot),mode(mode),dt(dt),Mots(4),Encs(8),PIDs(5){} void Core::addMOT(PinName plus,PinName minus,int period,int id){Mots.at(id) = new Motor(plus,minus,period,id);} void Core::addENC(PinName plus,PinName minus,int resolution,int mode,int id){Encs.at(id) = new Encoder(plus,minus,resolution,mode,id);} void Core::addPID(double Kp,double Ki,double Kd,int id){PIDs.at(id) = new PID(Kp,Ki,Kd,id);} void Core::setLIM(double MOT_max,double MOT_min,double PID_max,double PID_min){ Mots[rbt->RF]->setLimit(MOT_max,MOT_min); Mots[rbt->RB]->setLimit(MOT_max,MOT_min); Mots[rbt->LB]->setLimit(MOT_max,MOT_min); Mots[rbt->LF]->setLimit(MOT_max,MOT_min); PIDs[rbt->RF]->setLimit(PID_max,PID_min); PIDs[rbt->RB]->setLimit(PID_max,PID_min); PIDs[rbt->LB]->setLimit(PID_max,PID_min); PIDs[rbt->LF]->setLimit(PID_max,PID_min); } void Core::START(){timer.start();} bool Core::LOOP(){ bool ret = true; long long int temp; while(temp <= dt*1000.0*1000.0){ temp = timer.read_us()-t; if(temp < 0.0){ ret = false; printf("WARN!:Out of cycle:%lld(us)\n",temp); } } t = timer.read_us(); return ret; } void Core::WAIT(double wt){while(timer.read_us()-t <= wt*1000.0*1000.0);} void Core::setVelocity(double Vx,double Vy,double Vw){ double w1,w2,w3,w4; double k = sqrt(2.0)/2.0; switch(mode){ case OMNI4: w1 = -Vx*k*(sin(pos.theta)+cos(pos.theta)) - Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; w2 = -Vx*k*(sin(pos.theta)-cos(pos.theta)) + Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; w3 = Vx*k*(sin(pos.theta)+cos(pos.theta)) + Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; w4 = Vx*k*(sin(pos.theta)-cos(pos.theta)) - Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; break; case OMNI3: w1 = 0.0; w2 = 0.0; w3 = 0.0; w4 = 0.0; break; case MECANUM: w1 = 0.0; w2 = 0.0; w3 = 0.0; w4 = 0.0; break; } Encs[rbt->RF]->Update(dt); Encs[rbt->RB]->Update(dt); Encs[rbt->LB]->Update(dt); Encs[rbt->LF]->Update(dt); PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt); PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt); PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt); PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt); } Position Core::getStatus(){ double vx,vy,vw; double vX,vY; double Rw1,Rw2,Rw3,Rw4; Encs[rbt->F]->Update(dt); Encs[rbt->R]->Update(dt); Encs[rbt->B]->Update(dt); Encs[rbt->L]->Update(dt); Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR); Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR); Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR); Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR); vx = (-1*Rw1 + Rw3)/2.0; vy = (Rw2 + -1*Rw4)/2.0; vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0; vX = cos(pos.theta)*vx - sin(pos.theta)*vy; vY = sin(pos.theta)*vx + cos(pos.theta)*vy; pos.x += (vel.x+vX)*dt/2.0; pos.y += (vel.y+vY)*dt/2.0; pos.theta += (vel.theta+vw)*dt/2.0; vel.x = vX; vel.y = vY; vel.theta = vw; return pos; }