31 MULT_jointstate = {{JOINT_STATE_REPLACE}} 35 cmd_servo_state = SERVO_IDLE 36 cmd_servo_q = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 38 def set_servo_setpoint(q): 40 cmd_servo_state = SERVO_RUNNING 51 if (state == SERVO_RUNNING) and (cmd_servo_state == SERVO_IDLE): 54 state = cmd_servo_state 55 cmd_servo_state = SERVO_IDLE 60 elif state == SERVO_RUNNING: 61 servoj(q, {{SERVO_J_REPLACE}}) 68 socket_open("{{SERVER_IP_REPLACE}}", {{SERVER_PORT_REPLACE}}) 70 thread_servo = run servoThread() 73 params_mult = socket_read_binary_integer(6+1) 74 if params_mult[0] > 0: 75 q = [params_mult[1] / MULT_jointstate, params_mult[2] / MULT_jointstate, params_mult[3] / MULT_jointstate, params_mult[4] / MULT_jointstate, params_mult[5] / MULT_jointstate, params_mult[6] / MULT_jointstate] 76 keepalive = params_mult[7] 89 , commander_(commander)
90 , server_(reverse_port)
92 , servoj_lookahead_time_(0.03)
102 std::ostringstream out;
103 out <<
"t=" << std::fixed << std::setprecision(4) <<
servoj_time_;
114 LOG_ERROR(
"Failed to bind server, the port %d is likely already in use", reverse_port);
124 LOG_INFO(
"Uploading trajectory program to robot");
132 LOG_DEBUG(
"Awaiting incoming robot connection");
136 LOG_ERROR(
"Failed to accept incoming robot connection");
140 LOG_DEBUG(
"Robot successfully connected");
154 uint8_t buf[
sizeof(uint32_t) * 7];
157 for (
auto const &pos : positions)
164 int32_t val = htobe32(static_cast<int32_t>(keep_alive));
176 double c = (-3 * a + 3 * p1_pos - 2 * T * b - T * p1_vel) /
pow(T, 2);
177 double d = (2 * a - 2 * p1_pos + T * b + T * p1_vel) /
pow(T, 3);
178 return a + b * t + c *
pow(t, 2) + d *
pow(t, 3);
183 return execute(positions,
true);
191 using namespace std::chrono;
192 typedef duration<double> double_seconds;
193 typedef high_resolution_clock Clock;
194 typedef Clock::time_point Time;
196 auto &last = trajectory[trajectory.size() - 1];
197 auto &prev = trajectory[0];
199 Time t0 = Clock::now();
202 std::array<double, 6> positions;
204 for (
auto const &point : trajectory)
213 auto duration = point.time_from_start - prev.time_from_start;
214 double d_s = duration_cast<double_seconds>(duration).count();
219 latest = Clock::now();
220 auto elapsed = latest - t0;
222 if (point.time_from_start <= elapsed)
225 if (last.time_from_start <= elapsed)
228 double elapsed_s = duration_cast<double_seconds>(elapsed - prev.time_from_start).count();
229 for (
size_t j = 0; j < positions.size(); j++)
232 interpolate(elapsed_s, d_s, prev.positions[j], point.positions[j], prev.velocities[j], point.velocities[j]);
238 std::this_thread::sleep_for(std::chrono::milliseconds((
int)((
servoj_time_ * 1000) / 4.)));
248 return execute(last.positions,
true);
bool execute(std::array< double, 6 > &positions, bool keep_alive)
bool uploadProg(const std::string &s)
static const std::string SERVER_PORT_REPLACE("{{SERVER_PORT_REPLACE}}")
static const int32_t MULT_JOINTSTATE_
#define LOG_DEBUG(format,...)
static const std::string SERVO_J_REPLACE("{{SERVO_J_REPLACE}}")
bool write(const uint8_t *buf, size_t buf_len, size_t &written)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
double servoj_lookahead_time_
#define LOG_INFO(format,...)
double interpolate(double t, double T, double p0_pos, double p1_pos, double p0_vel, double p1_vel)
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
size_t append(uint8_t *buffer, T &val)
static const std::string JOINT_STATE_REPLACE("{{JOINT_STATE_REPLACE}}")
std::atomic< bool > running_
std::array< double, 6 > last_positions_
#define LOG_ERROR(format,...)
TrajectoryFollower(URCommander &commander, std::string &reverse_ip, int reverse_port, bool version_3)
static const std::string SERVER_IP_REPLACE("{{SERVER_IP_REPLACE}}")
static const std::string POSITION_PROGRAM