2022-12-01 17:30:05 +08:00
|
|
|
#include "unitree_arm_sdk/control/unitreeArm.h"
|
2022-09-13 19:52:44 +08:00
|
|
|
|
2022-11-16 16:23:36 +08:00
|
|
|
using namespace UNITREE_ARM;
|
|
|
|
|
2022-09-19 17:08:57 +08:00
|
|
|
class Z1ARM : public unitreeArm{
|
2022-09-13 19:52:44 +08:00
|
|
|
public:
|
2022-11-15 16:05:20 +08:00
|
|
|
Z1ARM():unitreeArm(true){};
|
2022-09-19 17:08:57 +08:00
|
|
|
~Z1ARM(){};
|
2022-09-13 19:52:44 +08:00
|
|
|
void armCtrlByFSM();
|
2022-09-19 17:08:57 +08:00
|
|
|
void armCtrlTrackInJointCtrl();
|
|
|
|
void armCtrlTrackInCartesian();
|
2022-11-11 18:17:07 +08:00
|
|
|
void printState();
|
2022-11-15 16:05:20 +08:00
|
|
|
private:
|
|
|
|
Vec6 qPast;
|
2022-09-13 19:52:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-09-19 17:08:57 +08:00
|
|
|
void Z1ARM::armCtrlByFSM() {
|
2022-09-13 19:52:44 +08:00
|
|
|
Vec6 posture[2];
|
|
|
|
|
|
|
|
std::cout << "[JOINTCTRL]" << std::endl;
|
|
|
|
setFsm(ArmFSMState::JOINTCTRL);
|
|
|
|
|
2022-11-11 18:17:07 +08:00
|
|
|
std::cout << "[TO STATE]" << std::endl;
|
2022-09-13 19:52:44 +08:00
|
|
|
labelRun("forward");
|
2022-11-11 18:17:07 +08:00
|
|
|
|
2022-09-13 19:52:44 +08:00
|
|
|
std::cout << "[MOVEJ]" << std::endl;
|
|
|
|
posture[0]<<0.5,0.1,0.1,0.5,-0.2,0.5;
|
2022-11-11 18:17:07 +08:00
|
|
|
MoveJ(posture[0], -M_PI/3.0, 1.0);
|
2022-09-13 19:52:44 +08:00
|
|
|
|
|
|
|
std::cout << "[MOVEL]" << std::endl;
|
|
|
|
posture[0] << 0,0,0,0.45,-0.2,0.2;
|
2022-11-11 18:17:07 +08:00
|
|
|
MoveL(posture[0], 0., 0.3);
|
2022-09-13 19:52:44 +08:00
|
|
|
|
|
|
|
std::cout << "[MOVEC]" << std::endl;
|
2022-11-11 18:17:07 +08:00
|
|
|
posture[0] << 0,0,0,0.45,0,0.4;
|
2022-09-13 19:52:44 +08:00
|
|
|
posture[1] << 0,0,0,0.45,0.2,0.2;
|
2022-11-11 18:17:07 +08:00
|
|
|
MoveC(posture[0], posture[1], -M_PI/3.0, 0.3);
|
2022-09-13 19:52:44 +08:00
|
|
|
}
|
|
|
|
|
2022-09-19 17:08:57 +08:00
|
|
|
void Z1ARM::armCtrlTrackInJointCtrl(){
|
2022-11-11 18:17:07 +08:00
|
|
|
labelRun("forward");
|
|
|
|
startTrack(ArmFSMState::JOINTCTRL);
|
2022-09-19 17:08:57 +08:00
|
|
|
for(;;){
|
2022-11-11 18:17:07 +08:00
|
|
|
q(3) -= _ctrlComp->dt * 1.0;//max dt*PI, rad/s
|
2022-09-13 19:52:44 +08:00
|
|
|
|
2022-11-15 16:05:20 +08:00
|
|
|
qPast = lowstate->getQ();
|
2022-11-11 18:17:07 +08:00
|
|
|
// std::cout << "qCmd: " << q.transpose() << " qState: " << qPast.transpose() << std::endl;
|
2022-09-19 17:08:57 +08:00
|
|
|
//The joint has reached limit, there is warning: joint cmd is far from state
|
2022-11-11 18:17:07 +08:00
|
|
|
double error = fabs(q(3) - qPast(3));
|
2022-09-19 17:08:57 +08:00
|
|
|
if(error > 0.1){
|
|
|
|
break;
|
|
|
|
}
|
2022-11-11 18:17:07 +08:00
|
|
|
usleep(_ctrlComp->dt*1000000);
|
2022-09-19 17:08:57 +08:00
|
|
|
}
|
2022-11-11 18:17:07 +08:00
|
|
|
_ctrlComp->sendCmd.track = false;
|
2022-09-19 17:08:57 +08:00
|
|
|
}
|
2022-09-13 19:52:44 +08:00
|
|
|
|
2022-09-19 17:08:57 +08:00
|
|
|
void Z1ARM::armCtrlTrackInCartesian(){
|
|
|
|
labelRun("forward");
|
2022-11-11 18:17:07 +08:00
|
|
|
startTrack(ArmFSMState::CARTESIAN);
|
2022-09-19 17:08:57 +08:00
|
|
|
for(;;){
|
2022-11-15 16:05:20 +08:00
|
|
|
endPosture(5) -= _ctrlComp->dt * 0.2;//z axis, m/s
|
2022-09-19 17:08:57 +08:00
|
|
|
|
|
|
|
// no inverse kinematics solution, the joint has reached limit
|
2022-12-01 17:30:05 +08:00
|
|
|
// std::cout << "postureCmd: " << endPosture.transpose() << " qState: " << lowstate->endPosture.transpose() << std::endl;
|
2022-11-15 16:05:20 +08:00
|
|
|
double error = fabs(endPosture(5) - lowstate->endPosture(5));
|
2022-09-19 17:08:57 +08:00
|
|
|
if( error > 0.1){
|
|
|
|
break;
|
|
|
|
}
|
2022-11-11 18:17:07 +08:00
|
|
|
usleep(_ctrlComp->dt*1000000);
|
2022-09-13 19:52:44 +08:00
|
|
|
}
|
2022-11-11 18:17:07 +08:00
|
|
|
_ctrlComp->sendCmd.track = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Z1ARM::printState(){
|
|
|
|
std::cout<<"------ joint State ------"<<std::endl;
|
2022-11-15 16:05:20 +08:00
|
|
|
std::cout<<"qState: "<<lowstate->getQ().transpose()<<std::endl;
|
|
|
|
std::cout<<"qdState: "<<lowstate->getQd().transpose()<<std::endl;
|
|
|
|
std::cout<<"tauState: "<<lowstate->getTau().transpose()<<std::endl;
|
2022-11-11 18:17:07 +08:00
|
|
|
|
|
|
|
std::cout<<"------ Endeffector Cartesian Posture ------"<<std::endl;
|
|
|
|
std::cout<<"roll pitch yaw x y z"<<std::endl;
|
2022-11-15 16:05:20 +08:00
|
|
|
std::cout<<lowstate->endPosture.transpose()<<std::endl;
|
2022-09-19 17:08:57 +08:00
|
|
|
}
|
2022-09-13 19:52:44 +08:00
|
|
|
|
2022-09-19 17:08:57 +08:00
|
|
|
int main() {
|
2022-11-15 16:05:20 +08:00
|
|
|
std::cout << std::fixed << std::setprecision(3);
|
|
|
|
Z1ARM arm;
|
2022-11-11 18:17:07 +08:00
|
|
|
arm.sendRecvThread->start();
|
2022-09-19 17:08:57 +08:00
|
|
|
|
|
|
|
arm.backToStart();
|
|
|
|
|
2022-12-01 17:30:05 +08:00
|
|
|
// size_t demo = 1;
|
2022-11-15 16:05:20 +08:00
|
|
|
for(size_t demo = 1; demo < 4; demo++)
|
2022-11-11 18:17:07 +08:00
|
|
|
switch (demo)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
arm.armCtrlByFSM();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
arm.armCtrlTrackInJointCtrl();
|
|
|
|
break;
|
2022-11-15 16:05:20 +08:00
|
|
|
case 3:
|
2022-11-11 18:17:07 +08:00
|
|
|
arm.armCtrlTrackInCartesian();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-09-19 17:08:57 +08:00
|
|
|
|
|
|
|
arm.backToStart();
|
2022-11-11 18:17:07 +08:00
|
|
|
arm.setFsm(ArmFSMState::PASSIVE);
|
|
|
|
arm.sendRecvThread->shutdown();
|
2022-09-13 19:52:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|