足回り動かすためのライブラリ
使用例
#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@3:3f42230ca4ec, 2021-10-13 (annotated)
- Committer:
- hamohamo
- Date:
- Wed Oct 13 08:47:03 2021 +0000
- Revision:
- 3:3f42230ca4ec
- Parent:
- 2:d88ff6dda390
- Child:
- 4:2425cd08e0c2
pe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hamohamo | 0:0a9ce35078a3 | 1 | #include "core.hpp" |
hamohamo | 0:0a9ce35078a3 | 2 | |
hamohamo | 3:3f42230ca4ec | 3 | Core::Core(Robot* robot,int mode,double dt):rbt(robot),mode(mode),dt(dt),Mots(5),Encs(9),PIDs(6){} |
hamohamo | 2:d88ff6dda390 | 4 | Motor* Core::addMOT(PinName plus,PinName minus,int period,int id){ |
hamohamo | 2:d88ff6dda390 | 5 | Mots.at(id) = new Motor(plus,minus,period,id); |
hamohamo | 2:d88ff6dda390 | 6 | return Mots.at(id); |
hamohamo | 2:d88ff6dda390 | 7 | } |
hamohamo | 0:0a9ce35078a3 | 8 | 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 | 9 | PID* Core::addPID(double Kp,double Ki,double Kd,int id){ |
hamohamo | 2:d88ff6dda390 | 10 | PIDs.at(id) = new PID(Kp,Ki,Kd,id); |
hamohamo | 2:d88ff6dda390 | 11 | return PIDs.at(id); |
hamohamo | 0:0a9ce35078a3 | 12 | } |
hamohamo | 2:d88ff6dda390 | 13 | void Core::setPWM(double pwm,int id){Mots[id]->setPWM(pwm);} |
hamohamo | 2:d88ff6dda390 | 14 | |
hamohamo | 0:0a9ce35078a3 | 15 | void Core::START(){timer.start();} |
hamohamo | 0:0a9ce35078a3 | 16 | bool Core::LOOP(){ |
hamohamo | 0:0a9ce35078a3 | 17 | bool ret = true; |
hamohamo | 0:0a9ce35078a3 | 18 | long long int temp; |
hamohamo | 0:0a9ce35078a3 | 19 | while(temp <= dt*1000.0*1000.0){ |
hamohamo | 0:0a9ce35078a3 | 20 | temp = timer.read_us()-t; |
hamohamo | 0:0a9ce35078a3 | 21 | if(temp < 0.0){ |
hamohamo | 0:0a9ce35078a3 | 22 | ret = false; |
hamohamo | 0:0a9ce35078a3 | 23 | printf("WARN!:Out of cycle:%lld(us)\n",temp); |
hamohamo | 0:0a9ce35078a3 | 24 | } |
hamohamo | 0:0a9ce35078a3 | 25 | } |
hamohamo | 0:0a9ce35078a3 | 26 | t = timer.read_us(); |
hamohamo | 0:0a9ce35078a3 | 27 | return ret; |
hamohamo | 0:0a9ce35078a3 | 28 | } |
hamohamo | 0:0a9ce35078a3 | 29 | void Core::WAIT(double wt){while(timer.read_us()-t <= wt*1000.0*1000.0);} |
hamohamo | 2:d88ff6dda390 | 30 | void Core::setPosition(double x,double y,double theta){ |
hamohamo | 2:d88ff6dda390 | 31 | pos.x = x; |
hamohamo | 2:d88ff6dda390 | 32 | pos.y = y; |
hamohamo | 2:d88ff6dda390 | 33 | pos.theta = theta; |
hamohamo | 2:d88ff6dda390 | 34 | } |
hamohamo | 0:0a9ce35078a3 | 35 | void Core::setVelocity(double Vx,double Vy,double Vw){ |
hamohamo | 0:0a9ce35078a3 | 36 | double w1,w2,w3,w4; |
hamohamo | 0:0a9ce35078a3 | 37 | double k = sqrt(2.0)/2.0; |
hamohamo | 0:0a9ce35078a3 | 38 | switch(mode){ |
hamohamo | 0:0a9ce35078a3 | 39 | case OMNI4: |
hamohamo | 0:0a9ce35078a3 | 40 | w1 = -Vx*k*(sin(pos.theta)+cos(pos.theta)) - Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 0:0a9ce35078a3 | 41 | w2 = -Vx*k*(sin(pos.theta)-cos(pos.theta)) + Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 0:0a9ce35078a3 | 42 | w3 = Vx*k*(sin(pos.theta)+cos(pos.theta)) + Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 0:0a9ce35078a3 | 43 | w4 = Vx*k*(sin(pos.theta)-cos(pos.theta)) - Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 0:0a9ce35078a3 | 44 | break; |
hamohamo | 0:0a9ce35078a3 | 45 | case OMNI3: |
hamohamo | 0:0a9ce35078a3 | 46 | w1 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 47 | w2 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 48 | w3 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 49 | w4 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 50 | break; |
hamohamo | 0:0a9ce35078a3 | 51 | case MECANUM: |
hamohamo | 0:0a9ce35078a3 | 52 | w1 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 53 | w2 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 54 | w3 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 55 | w4 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 56 | break; |
hamohamo | 0:0a9ce35078a3 | 57 | } |
hamohamo | 0:0a9ce35078a3 | 58 | Encs[rbt->RF]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 59 | Encs[rbt->RB]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 60 | Encs[rbt->LB]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 61 | Encs[rbt->LF]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 62 | PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 63 | PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 64 | PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 65 | PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 66 | setPWM(PIDs[rbt->RF]->getGain(),rbt->RF); |
hamohamo | 2:d88ff6dda390 | 67 | setPWM(PIDs[rbt->RB]->getGain(),rbt->RB); |
hamohamo | 2:d88ff6dda390 | 68 | setPWM(PIDs[rbt->LB]->getGain(),rbt->LB); |
hamohamo | 2:d88ff6dda390 | 69 | setPWM(PIDs[rbt->LF]->getGain(),rbt->LF); |
hamohamo | 0:0a9ce35078a3 | 70 | } |
hamohamo | 0:0a9ce35078a3 | 71 | Position Core::getStatus(){ |
hamohamo | 0:0a9ce35078a3 | 72 | double vx,vy,vw; |
hamohamo | 0:0a9ce35078a3 | 73 | double vX,vY; |
hamohamo | 0:0a9ce35078a3 | 74 | double Rw1,Rw2,Rw3,Rw4; |
hamohamo | 0:0a9ce35078a3 | 75 | Encs[rbt->F]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 76 | Encs[rbt->R]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 77 | Encs[rbt->B]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 78 | Encs[rbt->L]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 79 | |
hamohamo | 0:0a9ce35078a3 | 80 | Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR); |
hamohamo | 0:0a9ce35078a3 | 81 | Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR); |
hamohamo | 0:0a9ce35078a3 | 82 | Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR); |
hamohamo | 0:0a9ce35078a3 | 83 | Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR); |
hamohamo | 2:d88ff6dda390 | 84 | |
hamohamo | 0:0a9ce35078a3 | 85 | vx = (-1*Rw1 + Rw3)/2.0; |
hamohamo | 0:0a9ce35078a3 | 86 | vy = (Rw2 + -1*Rw4)/2.0; |
hamohamo | 0:0a9ce35078a3 | 87 | vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0; |
hamohamo | 0:0a9ce35078a3 | 88 | vX = cos(pos.theta)*vx - sin(pos.theta)*vy; |
hamohamo | 0:0a9ce35078a3 | 89 | vY = sin(pos.theta)*vx + cos(pos.theta)*vy; |
hamohamo | 2:d88ff6dda390 | 90 | |
hamohamo | 0:0a9ce35078a3 | 91 | pos.x += (vel.x+vX)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 92 | pos.y += (vel.y+vY)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 93 | pos.theta += (vel.theta+vw)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 94 | vel.x = vX; |
hamohamo | 0:0a9ce35078a3 | 95 | vel.y = vY; |
hamohamo | 0:0a9ce35078a3 | 96 | vel.theta = vw; |
hamohamo | 0:0a9ce35078a3 | 97 | return pos; |
hamohamo | 2:d88ff6dda390 | 98 | } |
hamohamo | 2:d88ff6dda390 | 99 | Core::Core(Robot* robot,ScrpSlave* scrp,int mode,double dt):rbt(robot),scrp(scrp),mode(mode),dt(dt),Mots(4),Encs(8),PIDs(5){} |
hamohamo | 2:d88ff6dda390 | 100 | void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100));} |
hamohamo | 2:d88ff6dda390 | 101 | void Core::sendVelocity(double Vx,double Vy,double Vw){ |
hamohamo | 2:d88ff6dda390 | 102 | double w1,w2,w3,w4; |
hamohamo | 2:d88ff6dda390 | 103 | double k = sqrt(2.0)/2.0; |
hamohamo | 2:d88ff6dda390 | 104 | switch(mode){ |
hamohamo | 2:d88ff6dda390 | 105 | case OMNI4: |
hamohamo | 2:d88ff6dda390 | 106 | w1 = -Vx*k*(sin(pos.theta)+cos(pos.theta)) - Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 2:d88ff6dda390 | 107 | w2 = -Vx*k*(sin(pos.theta)-cos(pos.theta)) + Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 2:d88ff6dda390 | 108 | w3 = Vx*k*(sin(pos.theta)+cos(pos.theta)) + Vy*k*(sin(pos.theta)-cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 2:d88ff6dda390 | 109 | w4 = Vx*k*(sin(pos.theta)-cos(pos.theta)) - Vy*k*(sin(pos.theta)+cos(pos.theta)) + rbt->C2CD*Vw; |
hamohamo | 2:d88ff6dda390 | 110 | break; |
hamohamo | 2:d88ff6dda390 | 111 | case OMNI3: |
hamohamo | 2:d88ff6dda390 | 112 | w1 = 0.0; |
hamohamo | 2:d88ff6dda390 | 113 | w2 = 0.0; |
hamohamo | 2:d88ff6dda390 | 114 | w3 = 0.0; |
hamohamo | 2:d88ff6dda390 | 115 | w4 = 0.0; |
hamohamo | 2:d88ff6dda390 | 116 | break; |
hamohamo | 2:d88ff6dda390 | 117 | case MECANUM: |
hamohamo | 2:d88ff6dda390 | 118 | w1 = 0.0; |
hamohamo | 2:d88ff6dda390 | 119 | w2 = 0.0; |
hamohamo | 2:d88ff6dda390 | 120 | w3 = 0.0; |
hamohamo | 2:d88ff6dda390 | 121 | w4 = 0.0; |
hamohamo | 2:d88ff6dda390 | 122 | break; |
hamohamo | 2:d88ff6dda390 | 123 | } |
hamohamo | 2:d88ff6dda390 | 124 | Encs[rbt->RF]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 125 | Encs[rbt->RB]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 126 | Encs[rbt->LB]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 127 | Encs[rbt->LF]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 128 | PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 129 | PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 130 | PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 131 | PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 132 | //printf("%lf,%lf\n",Encs[rbt->LF]->get_Omega(),w4/rbt->CWR); |
hamohamo | 2:d88ff6dda390 | 133 | WAIT(0.01); |
hamohamo | 3:3f42230ca4ec | 134 | sendPWM(PIDs[rbt->RF]->getGain(),rbt->RF); |
hamohamo | 2:d88ff6dda390 | 135 | WAIT(0.0125); |
hamohamo | 3:3f42230ca4ec | 136 | sendPWM(PIDs[rbt->RB]->getGain(),rbt->RB); |
hamohamo | 2:d88ff6dda390 | 137 | WAIT(0.015); |
hamohamo | 3:3f42230ca4ec | 138 | sendPWM(PIDs[rbt->LB]->getGain(),rbt->LB); |
hamohamo | 2:d88ff6dda390 | 139 | WAIT(0.0175); |
hamohamo | 3:3f42230ca4ec | 140 | sendPWM(PIDs[rbt->LF]->getGain(),rbt->LF); |
hamohamo | 0:0a9ce35078a3 | 141 | } |