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

使用例

#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:
Fri Oct 22 07:44:53 2021 +0000
Revision:
7:c0b6afbccd97
Parent:
6:87fd489a9801
Child:
8:475c17b23944
asd

Who changed what in which revision?

UserRevisionLine numberNew 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 0:0a9ce35078a3 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 0:0a9ce35078a3 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 6:87fd489a9801 68 setPWM(PIDs[rbt->RF]->getmv(),rbt->RF);
hamohamo 6:87fd489a9801 69 setPWM(PIDs[rbt->RB]->getmv(),rbt->RB);
hamohamo 6:87fd489a9801 70 setPWM(PIDs[rbt->LB]->getmv(),rbt->LB);
hamohamo 6:87fd489a9801 71 setPWM(PIDs[rbt->LF]->getmv(),rbt->LF);
hamohamo 0:0a9ce35078a3 72 }
hamohamo 6:87fd489a9801 73 Position Core::getStatus(bool onground){
hamohamo 0:0a9ce35078a3 74 double vx,vy,vw;
hamohamo 0:0a9ce35078a3 75 double vX,vY;
hamohamo 0:0a9ce35078a3 76 double Rw1,Rw2,Rw3,Rw4;
hamohamo 6:87fd489a9801 77 if(onground){
hamohamo 6:87fd489a9801 78 Encs[rbt->F]->Update(dt);
hamohamo 6:87fd489a9801 79 Encs[rbt->R]->Update(dt);
hamohamo 6:87fd489a9801 80 Encs[rbt->B]->Update(dt);
hamohamo 6:87fd489a9801 81 Encs[rbt->L]->Update(dt);
hamohamo 6:87fd489a9801 82
hamohamo 6:87fd489a9801 83 Rw1 = Encs[rbt->F]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 84 Rw2 = Encs[rbt->R]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 85 Rw3 = Encs[rbt->B]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 86 Rw4 = Encs[rbt->L]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 87 }
hamohamo 6:87fd489a9801 88 else {
hamohamo 6:87fd489a9801 89 Encs[rbt->RF]->Update(dt);
hamohamo 6:87fd489a9801 90 Encs[rbt->RB]->Update(dt);
hamohamo 6:87fd489a9801 91 Encs[rbt->LB]->Update(dt);
hamohamo 6:87fd489a9801 92 Encs[rbt->LF]->Update(dt);
hamohamo 6:87fd489a9801 93
hamohamo 6:87fd489a9801 94 Rw1 = Encs[rbt->RF]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 95 Rw2 = Encs[rbt->RB]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 96 Rw3 = Encs[rbt->LB]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 97 Rw4 = Encs[rbt->LF]->get_Omega()*(rbt->SWR);
hamohamo 6:87fd489a9801 98 }
hamohamo 0:0a9ce35078a3 99 vx = (-1*Rw1 + Rw3)/2.0;
hamohamo 0:0a9ce35078a3 100 vy = (Rw2 + -1*Rw4)/2.0;
hamohamo 0:0a9ce35078a3 101 vw = (Rw1 + Rw2 + Rw3 + Rw4)/(rbt->C2SD)/4.0;
hamohamo 6:87fd489a9801 102 if(onground){
hamohamo 6:87fd489a9801 103 vX = cos(pos.theta)*vx - sin(pos.theta)*vy;
hamohamo 6:87fd489a9801 104 vY = sin(pos.theta)*vx + cos(pos.theta)*vy;
hamohamo 6:87fd489a9801 105 }
hamohamo 6:87fd489a9801 106 else {
hamohamo 7:c0b6afbccd97 107 vX = cos(pos.theta-M_PI/4.0)*vx - sin(pos.theta-M_PI/4.0)*vy;
hamohamo 7:c0b6afbccd97 108 vY = sin(pos.theta-M_PI/4.0)*vx + cos(pos.theta-M_PI/4.0)*vy;
hamohamo 6:87fd489a9801 109 }
hamohamo 2:d88ff6dda390 110
hamohamo 0:0a9ce35078a3 111 pos.x += (vel.x+vX)*dt/2.0;
hamohamo 0:0a9ce35078a3 112 pos.y += (vel.y+vY)*dt/2.0;
hamohamo 0:0a9ce35078a3 113 pos.theta += (vel.theta+vw)*dt/2.0;
hamohamo 0:0a9ce35078a3 114 vel.x = vX;
hamohamo 0:0a9ce35078a3 115 vel.y = vY;
hamohamo 0:0a9ce35078a3 116 vel.theta = vw;
hamohamo 0:0a9ce35078a3 117 return pos;
hamohamo 2:d88ff6dda390 118 }
hamohamo 4:2425cd08e0c2 119 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 120 void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100.0));}
hamohamo 2:d88ff6dda390 121 void Core::sendVelocity(double Vx,double Vy,double Vw){
hamohamo 2:d88ff6dda390 122 double w1,w2,w3,w4;
hamohamo 7:c0b6afbccd97 123 double vx,vy;
hamohamo 7:c0b6afbccd97 124 vx = cos(pos.theta-M_PI/4.0)*Vx + sin(pos.theta-M_PI/4.0)*Vy;
hamohamo 7:c0b6afbccd97 125 vy = - sin(pos.theta-M_PI/4.0)*Vx + cos(pos.theta-M_PI/4.0)*Vy;
hamohamo 2:d88ff6dda390 126 switch(mode){
hamohamo 2:d88ff6dda390 127 case OMNI4:
hamohamo 7:c0b6afbccd97 128 w1 = -vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 129 w2 = vy + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 130 w3 = vx + rbt->C2CD*Vw;
hamohamo 7:c0b6afbccd97 131 w4 = -vy + rbt->C2CD*Vw;
hamohamo 2:d88ff6dda390 132 break;
hamohamo 2:d88ff6dda390 133 case OMNI3:
hamohamo 2:d88ff6dda390 134 w1 = 0.0;
hamohamo 2:d88ff6dda390 135 w2 = 0.0;
hamohamo 2:d88ff6dda390 136 w3 = 0.0;
hamohamo 2:d88ff6dda390 137 w4 = 0.0;
hamohamo 2:d88ff6dda390 138 break;
hamohamo 2:d88ff6dda390 139 case MECANUM:
hamohamo 2:d88ff6dda390 140 w1 = 0.0;
hamohamo 2:d88ff6dda390 141 w2 = 0.0;
hamohamo 2:d88ff6dda390 142 w3 = 0.0;
hamohamo 2:d88ff6dda390 143 w4 = 0.0;
hamohamo 2:d88ff6dda390 144 break;
hamohamo 2:d88ff6dda390 145 }
hamohamo 2:d88ff6dda390 146 Encs[rbt->RF]->Update(dt);
hamohamo 2:d88ff6dda390 147 Encs[rbt->RB]->Update(dt);
hamohamo 2:d88ff6dda390 148 Encs[rbt->LB]->Update(dt);
hamohamo 2:d88ff6dda390 149 Encs[rbt->LF]->Update(dt);
hamohamo 2:d88ff6dda390 150 PIDs[rbt->RF]->Update(Encs[rbt->RF]->get_Omega(),w1/rbt->CWR,dt);
hamohamo 2:d88ff6dda390 151 PIDs[rbt->RB]->Update(Encs[rbt->RB]->get_Omega(),w2/rbt->CWR,dt);
hamohamo 2:d88ff6dda390 152 PIDs[rbt->LB]->Update(Encs[rbt->LB]->get_Omega(),w3/rbt->CWR,dt);
hamohamo 2:d88ff6dda390 153 PIDs[rbt->LF]->Update(Encs[rbt->LF]->get_Omega(),w4/rbt->CWR,dt);
hamohamo 6:87fd489a9801 154 //printf("%lf,%lf,%lf\n",Encs[1]->get_Omega(),w1/rbt->CWR,PIDs[1]->getmv());
hamohamo 2:d88ff6dda390 155 WAIT(0.01);
hamohamo 6:87fd489a9801 156 sendPWM(PIDs[rbt->RF]->getmv(),rbt->RF);
hamohamo 2:d88ff6dda390 157 WAIT(0.0125);
hamohamo 6:87fd489a9801 158 sendPWM(PIDs[rbt->RB]->getmv(),rbt->RB);
hamohamo 2:d88ff6dda390 159 WAIT(0.015);
hamohamo 6:87fd489a9801 160 sendPWM(PIDs[rbt->LB]->getmv(),rbt->LB);
hamohamo 2:d88ff6dda390 161 WAIT(0.0175);
hamohamo 6:87fd489a9801 162 sendPWM(PIDs[rbt->LF]->getmv(),rbt->LF);
hamohamo 0:0a9ce35078a3 163 }