diff --git a/examples/SimpleSineMovement.cpp b/examples/SimpleSineMovement.cpp index 155c391..2683834 100644 --- a/examples/SimpleSineMovement.cpp +++ b/examples/SimpleSineMovement.cpp @@ -1,72 +1,89 @@ #include #include #include #include #include #include #include "liboncilla/Oncilla.h" #include "liboncilla/OncillaL1L2.h" #include "liboncilla/OncillaSynchronizer.h" using namespace std; using namespace boost; using namespace rci; using namespace rci::oncilla; +double hip_position(double phase,double amplitude,double offset){ + return amplitude * std::cos(phase) + offset; +} + +double knee_position(double phase, double phase_lag,double amplitude,double offset){ + return amplitude *((phase + phase_lag) > M_PI ? 1.0 : 0.0) + offset; +} + int main() { - double amplitude_rad = 1.0; - double frequency_Hz = 1.0; // Instantiate the actual robot object Oncilla oncilla; // Get the synchronizer, so that we can control the actual simulation // stepping from within our code OncillaSynchronizerPtr synchronizer = oncilla.getSynchronizer(); // Get all the joints we are interested in - L1 and L2 OncillaL1Ptr left_fore_hip = oncilla.getL1(Oncilla::LEFT_FORE); OncillaL2Ptr left_fore_knee = oncilla.getL2(Oncilla::LEFT_FORE); OncillaL1Ptr right_fore_hip = oncilla.getL1(Oncilla::RIGHT_FORE); OncillaL2Ptr right_fore_knee = oncilla.getL2(Oncilla::RIGHT_FORE); OncillaL1Ptr left_hind_hip = oncilla.getL1(Oncilla::LEFT_HIND); OncillaL2Ptr left_hind_knee = oncilla.getL2(Oncilla::LEFT_HIND); OncillaL1Ptr right_hind_hip = oncilla.getL1(Oncilla::RIGHT_HIND); OncillaL2Ptr right_hind_knee = oncilla.getL2(Oncilla::RIGHT_HIND); + //sets some parameter for the sinewave + double hip_amplitude(30), + fore_hip_offset(0), + hind_hip_offset(0), + fore_knee_amplitude(0.5), + fore_knee_offset(0), + hind_knee_amplitude(0), + hind_knee_offset(0), + fore_hip_knee_phase_lag(0), + hind_hip_knee_phase_lag(0), + frequency(1.5); // Now move the joints - double time_s, phase1, phase2; - JointAnglesPtr hip1, hip2, knee1, knee2; + double time_s, phase, antiphase; + + + time_s = 0; while (true) { // The time is our phase - TODO: Use virtual / simulated time - time_s = rsc::misc::currentTimeMicros() / 1000; - phase1 = time_s * frequency_Hz; - phase2 = (time_s + 0.5) * frequency_Hz; + time_s += synchronizer->latestProcessLoopDuration(); + phase = std::fmod(time_s * frequency * 2 * M_PI, 2 * M_PI); + + antiphase = std::fmod(phase + M_PI , 2 * M_PI); + + left_fore_hip->setJointPosition(JointAngles::fromDeg(hip_position(phase,hip_amplitude,fore_hip_offset))); + left_fore_knee->setJointPosition(JointAngles::fromRad(knee_position(phase,fore_hip_knee_phase_lag,fore_knee_amplitude,fore_knee_offset))); - hip1 = JointAngles::fromRad(amplitude_rad * sin(phase1)); - left_fore_hip->setJointPosition(hip1); - right_hind_hip->setJointPosition(hip1); + right_fore_hip->setJointPosition(JointAngles::fromDeg(hip_position(antiphase,hip_amplitude,fore_hip_offset))); + right_fore_knee->setJointPosition(JointAngles::fromRad(knee_position(antiphase,fore_hip_knee_phase_lag,fore_knee_amplitude,fore_knee_offset))); - hip2 = JointAngles::fromRad(amplitude_rad * sin(phase2)); - left_hind_hip->setJointPosition(hip2); - right_fore_hip->setJointPosition(hip2); + left_fore_hip->setJointPosition(JointAngles::fromDeg(hip_position(antiphase,hip_amplitude,hind_hip_offset))); + left_fore_knee->setJointPosition(JointAngles::fromRad(knee_position(antiphase,hind_hip_knee_phase_lag,hind_knee_amplitude,hind_knee_offset))); - knee1 = JointAngles::fromRad(amplitude_rad * sin(phase1)); - left_fore_knee->setJointPosition(knee1); - right_hind_knee->setJointPosition(knee1); + right_fore_hip->setJointPosition(JointAngles::fromDeg(hip_position(phase,hip_amplitude,hind_hip_offset))); + right_fore_knee->setJointPosition(JointAngles::fromRad(knee_position(phase,hind_hip_knee_phase_lag,hind_knee_amplitude,hind_knee_offset))); - knee2 = JointAngles::fromRad(amplitude_rad * sin(phase2)); - left_hind_knee->setJointPosition(knee2); - right_fore_knee->setJointPosition(knee2); // Now that we set the new angles, we do one processing step synchronizer->process(); } return EXIT_SUCCESS; }