46 # define M_PI 3.14159265358979323846
48 BigPuppyController::BigPuppyController(
double timestep) :
53 this->m_totalTime=0.0;
59 if (dt <= 0.0) {
throw std::invalid_argument(
"dt is not positive"); }
62 setBicepTargetLength(subject, dt);
63 setFrontTricepTargetLength(subject, dt);
64 setRearTricepTargetLength(subject, dt);
65 setLegToAbdomenTargetLength(subject, dt);
66 setRightShoulderTargetLength(subject, dt);
67 moveAllMotors(subject, dt);
74 void BigPuppyController::moveAllMotors(
BigPuppy& subject,
double dt){
76 const std::vector<tgSpringCableActuator*> muscles = subject.
getAllMuscles();
77 for (
size_t i = 0; i < muscles.size(); ++i) {
78 tgBasicActuator *
const pMuscle = tgCast::cast<tgSpringCableActuator, tgBasicActuator>(muscles[i]);
79 assert(pMuscle != NULL);
85 void BigPuppyController::setBicepTargetLength(
BigPuppy& subject,
double dt){
87 double newLengthMid = 0;
88 double newLengthOuter = 0;
89 const double bicepLength = 15.0;
91 const double amplitude1 = bicepLength/2;
92 const double amplitude2 = bicepLength/2;
93 const double angular_freq = 2;
94 const double phaseMid = 3*M_PI/2;
95 const double phaseOuter = phaseMid - M_PI;
96 const double dcOffset = bicepLength/2;
98 const std::vector<tgBasicActuator*> bicepMid = subject.
find<
tgBasicActuator>(
"right mid bicep");
99 const std::vector<tgBasicActuator*> bicepOuter = subject.
find<
tgBasicActuator>(
"right outer bicep");
100 const std::vector<tgBasicActuator*> bicepInner = subject.
find<
tgBasicActuator>(
"right inner bicep");
102 assert(bicepMid[0] != NULL);
103 assert(bicepOuter[0] != NULL);
104 assert(bicepInner[0] != NULL);
106 newLengthMid = amplitude1 * sin(angular_freq * m_totalTime + phaseMid) + dcOffset;
107 newLengthOuter = amplitude2 * sin(angular_freq * m_totalTime + phaseOuter) + 2*dcOffset;
109 bicepMid[0]->setControlInput(newLengthMid);
110 bicepOuter[0]->setControlInput(newLengthOuter);
111 bicepInner[0]->setControlInput(newLengthOuter);
115 void BigPuppyController::setFrontTricepTargetLength(
BigPuppy& subject,
double dt){
117 double newLengthInner = 0;
118 double newLengthOuter = 0;
119 const double frontTricepLength = sqrt(245.0);
121 const double amplitude = frontTricepLength/4;
122 const double angular_freq = 2;
123 const double phase1 = M_PI/2;
124 const double phase2 = 3*M_PI/2;
125 const double dcOffset = frontTricepLength;
128 const std::vector<tgBasicActuator*> frontOuterTricep = subject.
find<
tgBasicActuator>(
"outer right front tricep");
129 const std::vector<tgBasicActuator*> frontInnerTricep = subject.
find<
tgBasicActuator>(
"inner right front tricep");
131 assert(frontOuterTricep[0] != NULL);
132 assert(frontInnerTricep[0] != NULL);
134 newLengthInner = dcOffset - amplitude * sin(angular_freq * m_totalTime + phase1);
135 newLengthOuter = dcOffset - amplitude * sin(angular_freq * m_totalTime + phase2);
137 frontOuterTricep[0]->setControlInput(newLengthOuter);
138 frontInnerTricep[0]->setControlInput(newLengthInner);
142 void BigPuppyController::setRearTricepTargetLength(
BigPuppy& subject,
double dt){
144 double newLengthInner = 0;
145 double newLengthOuter = 0;
146 const double rearTricepLength = sqrt(165);
148 const double amplitude = rearTricepLength/4;
149 const double angular_freq = 2;
150 const double phase1 = 3*M_PI/2;
151 const double phase2 = M_PI/2;
152 const double dcOffset = rearTricepLength;
154 const std::vector<tgBasicActuator*> rearOuterTricep = subject.
find<
tgBasicActuator>(
"outer right tricep");
155 const std::vector<tgBasicActuator*> rearInnerTricep = subject.
find<
tgBasicActuator>(
"inner right tricep");
157 assert(rearOuterTricep[0] != NULL);
158 assert(rearInnerTricep[0] != NULL);
160 newLengthInner = dcOffset - amplitude * sin(angular_freq * m_totalTime + phase1);
161 newLengthOuter = dcOffset - amplitude * sin(angular_freq * m_totalTime + phase2);
163 rearOuterTricep[0]->setControlInput(newLengthOuter);
164 rearInnerTricep[0]->setControlInput(newLengthInner);
168 void BigPuppyController::setLegToAbdomenTargetLength(
BigPuppy& subject,
double dt){
170 double newLength = 0;
171 const double legToAbdomenLength = sqrt(66);
173 const double amplitude = legToAbdomenLength/1;
174 const double angular_freq = 2;
175 const double phase = 3*M_PI/2;
176 const double dcOffset = legToAbdomenLength;
178 const std::vector<tgBasicActuator*> legToAbdomen = subject.
find<
tgBasicActuator>(
"right front abdomen");
180 assert(legToAbdomen[0] != NULL);
182 newLength = amplitude * sin(angular_freq * m_totalTime + phase) + dcOffset;
184 legToAbdomen[0]->setControlInput(newLength);
189 void BigPuppyController::setRightShoulderTargetLength(
BigPuppy& subject,
double dt){
191 double newLengthFront = 0;
192 double newLengthRear = 0;
193 const double rightShoulderLength = sqrt(101);
195 const double amplitude = rightShoulderLength/2;
196 const double angular_freq = 2;
197 const double phaseRear = 3*M_PI/2;
198 const double phaseFront = M_PI/2;
199 const double dcOffset = rightShoulderLength;
201 const std::vector<tgBasicActuator*> rightShoulderFront = subject.
find<
tgBasicActuator>(
"right shoulder front mid");
202 const std::vector<tgBasicActuator*> rightShoulderRear = subject.
find<
tgBasicActuator>(
"right shoulder rear mid");
204 assert(rightShoulderFront[0] != NULL);
205 assert(rightShoulderRear[0] != NULL);
207 newLengthFront = dcOffset - amplitude * sin(angular_freq * m_totalTime + phaseFront);
208 newLengthRear = dcOffset - amplitude * sin(angular_freq * m_totalTime + phaseRear);
210 rightShoulderFront[0]->setControlInput(newLengthFront);
211 rightShoulderRear[0]->setControlInput(newLengthRear);
virtual void onStep(BigPuppy &subject, double dt)
Implementing a hand-tuned controller for a quadruped based roughly on the Flemons BigPuppy model...
virtual void moveMotors(double dt)
virtual void onSetup(BigPuppy &subject)
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
virtual void onTeardown(BigPuppy &subject)
Contains the definition of class tgBasicActuator.
std::vector< T * > find(const tgTagSearch &tagSearch)
Contains the definition of class tgKinematicActuator.
const std::vector< tgSpringCableActuator * > & getAllMuscles() const