足回り動かすためのライブラリ
使用例
#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-10-21
- Revision:
- 6:87fd489a9801
- Parent:
- 5:8dec5e011b3c
- Child:
- 7:c0b6afbccd97
File content as of revision 6:87fd489a9801:
#include "core.hpp" Core::Core(Robot* robot,int mode,double dt):rbt(robot),mode(mode),dt(dt),Mots(5),Encs(9),PIDs(6){} Motor* Core::addMOT(PinName plus,PinName minus,int period,int id){ Mots.at(id) = new Motor(plus,minus,period,id); return Mots.at(id); } void Core::addENC(PinName plus,PinName minus,int resolution,int mode,int id){Encs.at(id) = new Encoder(plus,minus,resolution,mode,id);} PID* Core::addPID(double Kp,double Ki,double Kd,int id){ PIDs.at(id) = new PID(Kp,Ki,Kd,id); return PIDs.at(id); } void Core::setPWM(double pwm,int id){Mots[id]->setPWM(pwm);} 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::setPosition(double x,double y,double theta){ pos.x = x; pos.y = y; pos.theta = theta; } 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); setPWM(PIDs[rbt->RF]->getmv(),rbt->RF); setPWM(PIDs[rbt->RB]->getmv(),rbt->RB); setPWM(PIDs[rbt->LB]->getmv(),rbt->LB); setPWM(PIDs[rbt->LF]->getmv(),rbt->LF); } Position Core::getStatus(bool onground){ double vx,vy,vw; double vX,vY; double Rw1,Rw2,Rw3,Rw4; if(onground){ 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); } else { Encs[rbt->RF]->Update(dt); Encs[rbt->RB]->Update(dt); Encs[rbt->LB]->Update(dt); Encs[rbt->LF]->Update(dt); Rw1 = Encs[rbt->RF]->get_Omega()*(rbt->SWR); Rw2 = Encs[rbt->RB]->get_Omega()*(rbt->SWR); Rw3 = Encs[rbt->LB]->get_Omega()*(rbt->SWR); Rw4 = Encs[rbt->LF]->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; if(onground){ vX = cos(pos.theta)*vx - sin(pos.theta)*vy; vY = sin(pos.theta)*vx + cos(pos.theta)*vy; } else { vX = cos(pos.theta-M_PI/2.0)*vx - sin(pos.theta-M_PI/2.0)*vy; vY = sin(pos.theta-M_PI/2.0)*vx + cos(pos.theta-M_PI/2.0)*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; } Core::Core(Robot* robot,ScrpSlave* scrp,int mode,double dt):rbt(robot),scrp(scrp),mode(mode),dt(dt),Mots(5),Encs(9),PIDs(6){} void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100.0));} void Core::sendVelocity(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); //printf("%lf,%lf,%lf\n",Encs[1]->get_Omega(),w1/rbt->CWR,PIDs[1]->getmv()); WAIT(0.01); sendPWM(PIDs[rbt->RF]->getmv(),rbt->RF); WAIT(0.0125); sendPWM(PIDs[rbt->RB]->getmv(),rbt->RB); WAIT(0.015); sendPWM(PIDs[rbt->LB]->getmv(),rbt->LB); WAIT(0.0175); sendPWM(PIDs[rbt->LF]->getmv(),rbt->LF); }