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

使用例

#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();
}
}
Revision:
2:d88ff6dda390
Parent:
0:0a9ce35078a3
Child:
3:3f42230ca4ec
--- a/core.cpp	Tue Sep 21 06:43:07 2021 +0000
+++ b/core.cpp	Wed Oct 13 08:12:34 2021 +0000
@@ -1,19 +1,17 @@
 #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);}
+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);}
-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);
+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;
@@ -29,6 +27,11 @@
     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;
@@ -60,6 +63,10 @@
     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]->getGain(),rbt->RF);
+    setPWM(PIDs[rbt->RB]->getGain(),rbt->RB);
+    setPWM(PIDs[rbt->LB]->getGain(),rbt->LB);
+    setPWM(PIDs[rbt->LF]->getGain(),rbt->LF);
 }
 Position Core::getStatus(){
     double vx,vy,vw;
@@ -69,15 +76,18 @@
     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;
@@ -85,4 +95,47 @@
     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(4),Encs(8),PIDs(5){}
+void Core::sendPWM(double pwm,int id){scrp->send1(255, id ,(pwm * 100));}
+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\n",Encs[rbt->LF]->get_Omega(),w4/rbt->CWR);
+    WAIT(0.01);
+    sendPWM(PIDs[rbt->RF]->getGain(),rbt->RF+1);
+    WAIT(0.0125);
+    sendPWM(PIDs[rbt->RB]->getGain(),rbt->RB+1);
+    WAIT(0.015);
+    sendPWM(PIDs[rbt->LB]->getGain(),rbt->LB+1);
+    WAIT(0.0175);
+    sendPWM(PIDs[rbt->LF]->getGain(),rbt->LF+1);
 }
\ No newline at end of file