|
| 1 | +#include <webots/brake.h> |
| 2 | +#include <webots/motor.h> |
| 3 | +#include <webots/nodes.h> |
| 4 | +#include <webots/robot.h> |
| 5 | +#include <webots/supervisor.h> |
| 6 | + |
| 7 | +#include "../../../lib/ts_assertion.h" |
| 8 | +#include "../../../lib/ts_utils.h" |
| 9 | + |
| 10 | +#include <math.h> |
| 11 | +#include <stdio.h> |
| 12 | + |
| 13 | +#define TIME_STEP 32 |
| 14 | + |
| 15 | +double angular_speed_of(WbNodeRef node) { |
| 16 | + const double *v = wb_supervisor_node_get_velocity(node); |
| 17 | + return sqrt(v[3] * v[3] + v[4] * v[4] + v[5] * v[5]); |
| 18 | +} |
| 19 | + |
| 20 | +struct HingeJoint2Data { |
| 21 | + WbDeviceTag driveMotor; |
| 22 | + WbNodeRef shaftNode; |
| 23 | +}; |
| 24 | + |
| 25 | +void init(struct HingeJoint2Data *jd, char *driveMotorName, char *shaftDef) { |
| 26 | + jd->driveMotor = wb_robot_get_device(driveMotorName); |
| 27 | + wb_motor_set_position(jd->driveMotor, INFINITY); |
| 28 | + wb_motor_set_velocity(jd->driveMotor, 0.0); |
| 29 | + wb_brake_set_damping_constant(wb_motor_get_brake(jd->driveMotor), 28.0); |
| 30 | + jd->shaftNode = wb_supervisor_node_get_from_def(shaftDef); |
| 31 | +} |
| 32 | + |
| 33 | +void assert_same_speed_for_secs(struct HingeJoint2Data j1, struct HingeJoint2Data j2, double durationSecs) { |
| 34 | + const double tolerance = 0.001; |
| 35 | + |
| 36 | + double t = 0; |
| 37 | + while (wb_robot_step(TIME_STEP) != -1 && t < durationSecs) { |
| 38 | + t += (double)TIME_STEP / 1000.0; |
| 39 | + double expSpeed = angular_speed_of(j1.shaftNode); |
| 40 | + double actSpeed = angular_speed_of(j2.shaftNode); |
| 41 | + ts_assert_double_in_delta(actSpeed, expSpeed, tolerance, "Unexpected angle rate (expected = %lf, measured = %lf)", expSpeed, |
| 42 | + actSpeed); |
| 43 | + printf("angle_rate: %lf expected: %lf\n", actSpeed, expSpeed); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +int main(int argc, char **argv) { |
| 48 | + ts_setup(argv[0]); |
| 49 | + |
| 50 | + struct HingeJoint2Data joint1, joint2; |
| 51 | + |
| 52 | + init(&joint1, "joint1motor", "HINGE2JOINT1_SHAFT"); |
| 53 | + init(&joint2, "joint2motor", "HINGE2JOINT2_SHAFT"); |
| 54 | + |
| 55 | + // Rotate the first axis of the second joint by 90 degrees |
| 56 | + // (takes about 1 second) |
| 57 | + wb_motor_set_position(wb_robot_get_device("joint2turnmotor"), 1.57); |
| 58 | + double t = 0.0; // elapsed simulation time |
| 59 | + while (wb_robot_step(TIME_STEP) != -1 && t < 1.0) { |
| 60 | + t += (double)TIME_STEP / 1000.0; |
| 61 | + } |
| 62 | + |
| 63 | + // Accerate toward max velocity for 1 second. |
| 64 | + wb_motor_set_velocity(joint1.driveMotor, 6.28); |
| 65 | + wb_motor_set_velocity(joint2.driveMotor, 6.28); |
| 66 | + assert_same_speed_for_secs(joint1, joint2, 1.0); |
| 67 | + |
| 68 | + // Coast to a stop |
| 69 | + wb_motor_set_velocity(joint1.driveMotor, 0.0); |
| 70 | + wb_motor_set_available_torque(joint1.driveMotor, 0.0); |
| 71 | + wb_motor_set_velocity(joint2.driveMotor, 0.0); |
| 72 | + wb_motor_set_available_torque(joint2.driveMotor, 0.0); |
| 73 | + assert_same_speed_for_secs(joint1, joint2, 1.0); |
| 74 | + |
| 75 | + ts_send_success(); |
| 76 | + return EXIT_SUCCESS; |
| 77 | +} |
0 commit comments