test-BeoSubPIDRot.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "Devices/BeoChip.H"
00040 #include "BeoSub/BeoSubIMU.H"
00041 #include "BeoSub/BeoSubBallast.H"
00042 #include "Controllers/PID.H"
00043
00044 #include <cstdlib>
00045 #include <iostream>
00046 #include <pthread.h>
00047
00048 nub::soft_ref<BeoChip> bc_ptr;
00049 PID<float> *pid;
00050
00051 float cvel;
00052 float cdelta;
00053 bool start = true;
00054
00055
00056 void thrust(const float vel, const float delta) {
00057
00058 float left = vel+delta;
00059 if(left > 1) left = 1;
00060 else if(left < -1) left = -1;
00061 float right = vel-delta;
00062 if(right > 1) right = 1;
00063 else if(right < -1) right = -1;
00064 bc_ptr->setServo(3, left);
00065 bc_ptr->setServo(2, right);
00066
00067
00068 }
00069
00070
00071 class IMUListener : public BeoSubIMUListener {
00072 public:
00073
00074 virtual ~IMUListener() {};
00075
00076
00077 virtual void newData(const float xa, const float ya, const float za,
00078 const Angle xv, const Angle yv, const Angle zv)
00079 {
00080 float current = zv.getVal();
00081
00082 cdelta = pid->update(0, current);
00083 LINFO("%f %f", current, cdelta);
00084
00085 if(start) {
00086
00087 thrust(cvel, cdelta);
00088 }
00089 }
00090 };
00091
00092
00093 int main(const int argc, const char* argv[])
00094 {
00095 MYLOGVERB = LOG_INFO;
00096
00097
00098 ModelManager manager("BeoSubPIDRot test program");
00099
00100
00101 nub::soft_ref<BeoChip> bc(new BeoChip(manager));
00102 manager.addSubComponent(bc);
00103 bc_ptr = bc;
00104
00105
00106 nub::soft_ref<BeoSubIMU> imu(new BeoSubIMU(manager));
00107 manager.addSubComponent(imu);
00108
00109
00110 pid = new PID<float>(0.5, 0.001, 0, -500, 500);
00111
00112
00113 if (manager.parseCommandLine(argc, argv, "<BeoChip> <IMU>", 2, 2) == false)
00114 return(1);
00115
00116
00117 bc->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0));
00118 bc->setModelParamVal("BeoChipUseRTSCTS", false);
00119 imu->setModelParamVal("IMUSerialPortDevName", manager.getExtraArg(1), MC_RECURSE);
00120
00121 rutz::shared_ptr<IMUListener> lis(new IMUListener);
00122 rutz::shared_ptr<BeoSubIMUListener> lis2; lis2.dynCastFrom(lis);
00123 imu->setListener(lis2);
00124
00125
00126 manager.start();
00127
00128 LINFO("Reseting the BeoChip...");
00129 bc->reset(MC_RECURSE);
00130 sleep(1);
00131
00132 LINFO("Waiting for a bit. Turn BeoChip on if not already on."); sleep(1);
00133 LINFO("Echo request (should bring an echo reply back)...");
00134 bc->echoRequest(); sleep(1);
00135
00136 cvel = 0.8;
00137 char c;
00138 std::cin >> c;
00139 start = false;
00140
00141
00142 manager.stop();
00143 thrust(0, 0);
00144 return 0;
00145 }
00146
00147
00148
00149
00150
00151