BeeStemSim.C
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 "SeaBee/BeeStemSim.H"
00039
00040 #include "Component/OptionManager.H"
00041 #include "Devices/Serial.H"
00042 #include "Image/MatrixOps.H"
00043 #include <string>
00044
00045 #define BS_CMD_DELAY 5000000
00046
00047 namespace
00048 {
00049 class SubSimLoop : public JobWithSemaphore
00050 {
00051 public:
00052 SubSimLoop(BeeStemSim* beeStemSim)
00053 :
00054 itsBeeStemSim(beeStemSim),
00055 itsPriority(1),
00056 itsJobType("controllerLoop")
00057 {}
00058
00059 virtual ~SubSimLoop() {}
00060
00061 virtual void run()
00062 {
00063 ASSERT(itsBeeStemSim);
00064 while(1)
00065 {
00066 itsBeeStemSim->simLoop();
00067 usleep(1000);
00068 }
00069 }
00070
00071 virtual const char* jobType() const
00072 { return itsJobType.c_str(); }
00073
00074 virtual int priority() const
00075 { return itsPriority; }
00076
00077 private:
00078 BeeStemSim* itsBeeStemSim;
00079 const int itsPriority;
00080 const std::string itsJobType;
00081 };
00082 }
00083
00084
00085
00086
00087 BeeStemSim::BeeStemSim(OptionManager& mgr, const std::string& descrName,
00088 const std::string& tagName) :
00089 ModelComponent(mgr, descrName, tagName),
00090 itsSubSim(new SubSim(mgr))
00091 {
00092
00093 addSubComponent(itsSubSim);
00094
00095 for (int i=0; i<5; i++)
00096 itsLastMotorCmd[i] = 0;
00097
00098 initRandomNumbers();
00099 }
00100
00101
00102 BeeStemSim::~BeeStemSim()
00103 {
00104 }
00105
00106 void BeeStemSim::start2()
00107 {
00108
00109 itsThreadServer.reset(new WorkThreadServer("SubSim",1));
00110 itsThreadServer->setFlushBeforeStopping(false);
00111 rutz::shared_ptr<SubSimLoop> j(new SubSimLoop(this));
00112 itsThreadServer->enqueueJob(j);
00113 }
00114
00115 void BeeStemSim::simLoop()
00116 {
00117 itsSubSim->simLoop();
00118
00119
00120
00121 }
00122
00123
00124 bool BeeStemSim::setThrusters(int &m1, int &m2, int &m3,
00125 int &m4, int &m5)
00126 {
00127
00128
00129
00130
00131 if (m1 > MOTOR_MAX) m1 = MOTOR_MAX; if (m1 < -MOTOR_MAX) m1 = -MOTOR_MAX;
00132 if (m2 > MOTOR_MAX) m2 = MOTOR_MAX; if (m2 < -MOTOR_MAX) m2 = -MOTOR_MAX;
00133 if (m3 > MOTOR_MAX) m3 = MOTOR_MAX; if (m3 < -MOTOR_MAX) m3 = -MOTOR_MAX;
00134 if (m4 > MOTOR_MAX) m4 = MOTOR_MAX; if (m4 < -MOTOR_MAX) m4 = -MOTOR_MAX;
00135 if (m5 > MOTOR_MAX) m5 = MOTOR_MAX; if (m5 < -MOTOR_MAX) m5 = -MOTOR_MAX;
00136
00137
00138
00139 if (abs(itsLastMotorCmd[0] - m1) > 60 && itsLastMotorCmd[0]*m1 < 0) m1 = 0;
00140 if (abs(itsLastMotorCmd[1] - m2) > 60 && itsLastMotorCmd[1]*m2 < 0) m2 = 0;
00141 if (abs(itsLastMotorCmd[2] - m3) > 60 && itsLastMotorCmd[2]*m3 < 0) m3 = 0;
00142 if (abs(itsLastMotorCmd[3] - m4) > 60 && itsLastMotorCmd[3]*m4 < 0) m4 = 0;
00143 if (abs(itsLastMotorCmd[4] - m5) > 60 && itsLastMotorCmd[4]*m5 < 0) m5 = 0;
00144
00145
00146 itsLastMotorCmd[0] = m1;
00147 itsLastMotorCmd[1] = m2;
00148 itsLastMotorCmd[2] = m3;
00149 itsLastMotorCmd[3] = m4;
00150 itsLastMotorCmd[4] = m5;
00151
00152 float pan = ((float(m2) - float(m4))/2) / 100.0;
00153 float tilt = 0;
00154 float forward = -1*((float(m2) + float(m4))/2) / 100.0;
00155 float up = -1*((float(m1) + float(m3) + float(m5))/3.0)/100.0;
00156
00157
00158 itsSubSim->setTrusters(pan, tilt, forward, up);
00159
00160
00161 return true;
00162
00163 }
00164
00165
00166 bool BeeStemSim::getSensors(int &heading, int &pitch, int &roll, int &ext_pressure, int &int_pressure)
00167 {
00168 float simxPos;
00169 float simyPos;
00170 float simdepth;
00171 float simroll;
00172 float simpitch;
00173 float simyaw;
00174
00175 int heading_noise = 0;
00176 int pitch_noise = randomUpToIncluding(6) - 3;
00177 int roll_noise = randomUpToIncluding(6) - 3;
00178 int ext_p_noise = randomUpToIncluding(6) - 3;
00179 int int_p_noise = randomUpToIncluding(6) - 3;
00180
00181 itsSubSim->getSensors(simxPos, simyPos, simdepth,
00182 simroll, simpitch, simyaw);
00183
00184 heading = (int)(simyaw*180/M_PI) + heading_noise;
00185 pitch = (int)(simpitch*180/M_PI) + pitch_noise;
00186 roll = (int)(simroll*180/M_PI) + roll_noise;
00187
00188 ext_pressure = (int)(simdepth*100) + ext_p_noise;
00189
00190 int_pressure = 90 + int_p_noise;
00191
00192
00193 return true;
00194 }
00195
00196 bool BeeStemSim::setHeartBeat()
00197 {
00198 return true;
00199
00200 }
00201
00202 Image<PixRGB<byte> > BeeStemSim::getImage(int camera)
00203 {
00204
00205
00206
00207
00208
00209
00210 return flipVertic(itsSubSim->getFrame(camera));
00211
00212
00213 }
00214
00215
00216
00217
00218
00219