足回り動かすためのライブラリ
使用例
#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(); } }
Diff: pid.cpp
- Revision:
- 6:87fd489a9801
- Parent:
- 5:8dec5e011b3c
- Child:
- 11:80800fd9f4af
diff -r 8dec5e011b3c -r 87fd489a9801 pid.cpp --- a/pid.cpp Mon Oct 18 08:15:39 2021 +0000 +++ b/pid.cpp Thu Oct 21 16:16:01 2021 +0000 @@ -1,20 +1,31 @@ #include "pid.hpp" #include "mbed.h" PID::PID(double kp,double ki,double kd,int id):Kp(kp),Ki(ki),Kd(kd),ID(id){ - err[0]=0.0; + e[0]=0.0; mv = 0.0; integral = 0.0; } -void PID::Update(double Value,double Target,double dt){ +void PID::Update(double Value,double Target,double dt,bool addmv){/* e[2]=Target-Value; P=(e[2]-e[1])/dt; I= e[2]; - D=(e[2]- 2*e[1] + e[0)/dt/dt; + D=(e[2]- 2*e[1] + e[0])/dt/dt; e[0] = e[1]; e[1] = e[2]; - mv += ((Kp*P)+(Ki*I)+(Kd*D))*dt; + integral += ((Kp*P)+(Ki*I)+(Kd*D))*dt; + mv += integral;*/ + e[2] = Target - Value; + P = e[2]; + integral += (e[2]+e[1])*dt/2.0; + I = integral; + D = (e[2]-e[1])/dt; + if(addmv) mv += ((Kp*P)+(Ki*I)+(Kd*D)); + else mv = ((Kp*P)+(Ki*I)+(Kd*D)); + e[1] = e[2]; if(mv<Min) mv = Min; if(mv>Max) mv = Max; + if(integral<Min) integral = Min; + if(integral>Max) integral = Max; } double PID::getmv(){ double rtn;