足回り動かすためのライブラリ
使用例
#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@8:475c17b23944, 2021-10-22 (annotated)
- Committer:
- hamohamo
- Date:
- Fri Oct 22 09:41:23 2021 +0000
- Revision:
- 8:475c17b23944
- Parent:
- 7:c0b6afbccd97
- Child:
- 9:489046c1e624
asd
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 | 7:c0b6afbccd97 | 37 | double vx,vy; |
hamohamo | 7:c0b6afbccd97 | 38 | vx = cos(pos.theta-M_PI/4.0)*Vx + sin(pos.theta-M_PI/4.0)*Vy; |
hamohamo | 7:c0b6afbccd97 | 39 | vy = - sin(pos.theta-M_PI/4.0)*Vx + cos(pos.theta-M_PI/4.0)*Vy; |
hamohamo | 0:0a9ce35078a3 | 40 | switch(mode){ |
hamohamo | 0:0a9ce35078a3 | 41 | case OMNI4: |
hamohamo | 7:c0b6afbccd97 | 42 | w1 = -vx + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 43 | w2 = vy + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 44 | w3 = vx + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 45 | w4 = -vy + rbt->C2CD*Vw; |
hamohamo | 0:0a9ce35078a3 | 46 | break; |
hamohamo | 0:0a9ce35078a3 | 47 | case OMNI3: |
hamohamo | 0:0a9ce35078a3 | 48 | w1 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 49 | w2 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 50 | w3 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 51 | w4 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 52 | break; |
hamohamo | 0:0a9ce35078a3 | 53 | case MECANUM: |
hamohamo | 0:0a9ce35078a3 | 54 | w1 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 55 | w2 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 56 | w3 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 57 | w4 = 0.0; |
hamohamo | 0:0a9ce35078a3 | 58 | break; |
hamohamo | 8:475c17b23944 | 59 | }/* |
hamohamo | 0:0a9ce35078a3 | 60 | Encs[rbt->RF]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 61 | Encs[rbt->RB]->Update(dt); |
hamohamo | 0:0a9ce35078a3 | 62 | Encs[rbt->LB]->Update(dt); |
hamohamo | 8:475c17b23944 | 63 | Encs[rbt->LF]->Update(dt);*/ |
hamohamo | 0:0a9ce35078a3 | 64 | PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 65 | PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 66 | PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt); |
hamohamo | 0:0a9ce35078a3 | 67 | PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt); |
hamohamo | 8:475c17b23944 | 68 | |
hamohamo | 6:87fd489a9801 | 69 | setPWM(PIDs[rbt->RF]->getmv(),rbt->RF); |
hamohamo | 6:87fd489a9801 | 70 | setPWM(PIDs[rbt->RB]->getmv(),rbt->RB); |
hamohamo | 6:87fd489a9801 | 71 | setPWM(PIDs[rbt->LB]->getmv(),rbt->LB); |
hamohamo | 6:87fd489a9801 | 72 | setPWM(PIDs[rbt->LF]->getmv(),rbt->LF); |
hamohamo | 0:0a9ce35078a3 | 73 | } |
hamohamo | 6:87fd489a9801 | 74 | Position Core::getStatus(bool onground){ |
hamohamo | 0:0a9ce35078a3 | 75 | double vx,vy,vw; |
hamohamo | 0:0a9ce35078a3 | 76 | double vX,vY; |
hamohamo | 0:0a9ce35078a3 | 77 | double Rw1,Rw2,Rw3,Rw4; |
hamohamo | 8:475c17b23944 | 78 | Encs[rbt->F]->Update(dt); |
hamohamo | 8:475c17b23944 | 79 | Encs[rbt->R]->Update(dt); |
hamohamo | 8:475c17b23944 | 80 | Encs[rbt->B]->Update(dt); |
hamohamo | 8:475c17b23944 | 81 | Encs[rbt->L]->Update(dt); |
hamohamo | 8:475c17b23944 | 82 | Encs[rbt->RF]->Update(dt); |
hamohamo | 8:475c17b23944 | 83 | Encs[rbt->RB]->Update(dt); |
hamohamo | 8:475c17b23944 | 84 | Encs[rbt->LB]->Update(dt); |
hamohamo | 8:475c17b23944 | 85 | Encs[rbt->LF]->Update(dt); |
hamohamo | 6:87fd489a9801 | 86 | if(onground){ |
hamohamo | 6:87fd489a9801 | 87 | |
hamohamo | 6:87fd489a9801 | 88 | Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 89 | Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 90 | Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 91 | Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 92 | } |
hamohamo | 6:87fd489a9801 | 93 | else { |
hamohamo | 6:87fd489a9801 | 94 | |
hamohamo | 6:87fd489a9801 | 95 | Rw1 = Encs[rbt->RF]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 96 | Rw2 = Encs[rbt->RB]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 97 | Rw3 = Encs[rbt->LB]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 98 | Rw4 = Encs[rbt->LF]->get_Omega()*(rbt->SWR); |
hamohamo | 6:87fd489a9801 | 99 | } |
hamohamo | 0:0a9ce35078a3 | 100 | vx = (-1*Rw1 + Rw3)/2.0; |
hamohamo | 0:0a9ce35078a3 | 101 | vy = (Rw2 + -1*Rw4)/2.0; |
hamohamo | 0:0a9ce35078a3 | 102 | vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0; |
hamohamo | 6:87fd489a9801 | 103 | if(onground){ |
hamohamo | 6:87fd489a9801 | 104 | vX = cos(pos.theta)*vx - sin(pos.theta)*vy; |
hamohamo | 6:87fd489a9801 | 105 | vY = sin(pos.theta)*vx + cos(pos.theta)*vy; |
hamohamo | 6:87fd489a9801 | 106 | } |
hamohamo | 6:87fd489a9801 | 107 | else { |
hamohamo | 7:c0b6afbccd97 | 108 | vX = cos(pos.theta-M_PI/4.0)*vx - sin(pos.theta-M_PI/4.0)*vy; |
hamohamo | 7:c0b6afbccd97 | 109 | vY = sin(pos.theta-M_PI/4.0)*vx + cos(pos.theta-M_PI/4.0)*vy; |
hamohamo | 6:87fd489a9801 | 110 | } |
hamohamo | 2:d88ff6dda390 | 111 | |
hamohamo | 0:0a9ce35078a3 | 112 | pos.x += (vel.x+vX)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 113 | pos.y += (vel.y+vY)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 114 | pos.theta += (vel.theta+vw)*dt/2.0; |
hamohamo | 0:0a9ce35078a3 | 115 | vel.x = vX; |
hamohamo | 0:0a9ce35078a3 | 116 | vel.y = vY; |
hamohamo | 0:0a9ce35078a3 | 117 | vel.theta = vw; |
hamohamo | 0:0a9ce35078a3 | 118 | return pos; |
hamohamo | 2:d88ff6dda390 | 119 | } |
hamohamo | 4:2425cd08e0c2 | 120 | 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 | 121 | void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100.0));} |
hamohamo | 2:d88ff6dda390 | 122 | void Core::sendVelocity(double Vx,double Vy,double Vw){ |
hamohamo | 2:d88ff6dda390 | 123 | double w1,w2,w3,w4; |
hamohamo | 7:c0b6afbccd97 | 124 | double vx,vy; |
hamohamo | 7:c0b6afbccd97 | 125 | vx = cos(pos.theta-M_PI/4.0)*Vx + sin(pos.theta-M_PI/4.0)*Vy; |
hamohamo | 7:c0b6afbccd97 | 126 | vy = - sin(pos.theta-M_PI/4.0)*Vx + cos(pos.theta-M_PI/4.0)*Vy; |
hamohamo | 2:d88ff6dda390 | 127 | switch(mode){ |
hamohamo | 2:d88ff6dda390 | 128 | case OMNI4: |
hamohamo | 7:c0b6afbccd97 | 129 | w1 = -vx + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 130 | w2 = vy + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 131 | w3 = vx + rbt->C2CD*Vw; |
hamohamo | 7:c0b6afbccd97 | 132 | w4 = -vy + rbt->C2CD*Vw; |
hamohamo | 2:d88ff6dda390 | 133 | break; |
hamohamo | 2:d88ff6dda390 | 134 | case OMNI3: |
hamohamo | 2:d88ff6dda390 | 135 | w1 = 0.0; |
hamohamo | 2:d88ff6dda390 | 136 | w2 = 0.0; |
hamohamo | 2:d88ff6dda390 | 137 | w3 = 0.0; |
hamohamo | 2:d88ff6dda390 | 138 | w4 = 0.0; |
hamohamo | 2:d88ff6dda390 | 139 | break; |
hamohamo | 2:d88ff6dda390 | 140 | case MECANUM: |
hamohamo | 2:d88ff6dda390 | 141 | w1 = 0.0; |
hamohamo | 2:d88ff6dda390 | 142 | w2 = 0.0; |
hamohamo | 2:d88ff6dda390 | 143 | w3 = 0.0; |
hamohamo | 2:d88ff6dda390 | 144 | w4 = 0.0; |
hamohamo | 2:d88ff6dda390 | 145 | break; |
hamohamo | 8:475c17b23944 | 146 | }/* |
hamohamo | 2:d88ff6dda390 | 147 | Encs[rbt->RF]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 148 | Encs[rbt->RB]->Update(dt); |
hamohamo | 2:d88ff6dda390 | 149 | Encs[rbt->LB]->Update(dt); |
hamohamo | 8:475c17b23944 | 150 | Encs[rbt->LF]->Update(dt);*/ |
hamohamo | 2:d88ff6dda390 | 151 | PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 152 | PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 153 | PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt); |
hamohamo | 2:d88ff6dda390 | 154 | PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt); |
hamohamo | 6:87fd489a9801 | 155 | //printf("%lf,%lf,%lf\n",Encs[1]->get_Omega(),w1/rbt->CWR,PIDs[1]->getmv()); |
hamohamo | 2:d88ff6dda390 | 156 | WAIT(0.01); |
hamohamo | 6:87fd489a9801 | 157 | sendPWM(PIDs[rbt->RF]->getmv(),rbt->RF); |
hamohamo | 2:d88ff6dda390 | 158 | WAIT(0.0125); |
hamohamo | 6:87fd489a9801 | 159 | sendPWM(PIDs[rbt->RB]->getmv(),rbt->RB); |
hamohamo | 2:d88ff6dda390 | 160 | WAIT(0.015); |
hamohamo | 6:87fd489a9801 | 161 | sendPWM(PIDs[rbt->LB]->getmv(),rbt->LB); |
hamohamo | 2:d88ff6dda390 | 162 | WAIT(0.0175); |
hamohamo | 6:87fd489a9801 | 163 | sendPWM(PIDs[rbt->LF]->getmv(),rbt->LF); |
hamohamo | 0:0a9ce35078a3 | 164 | } |