Skip to content

Commit 5016072

Browse files
committed
Fixes for supporting simulator; switch to mosquitto_loop_start interface
1 parent 3c8e18e commit 5016072

File tree

6 files changed

+62
-58
lines changed

6 files changed

+62
-58
lines changed

c-cnc23.code-workspace

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"*.h": "c",
1414
"*.rmd": "markdown",
1515
"*.hh": "c++",
16-
"__node_handle": "c"
16+
"__node_handle": "c",
17+
"cmath": "c",
18+
"complex": "c"
1719
}
1820
},
1921
"launch": {

machine.ini

+29-29
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ A = 5.0
55
max_error = 0.005
66
# Sampling time (in seconds!)
77
tq = 0.005
8-
# Machine initial position
9-
zero = [0, 0, 500]
108
# Workpiece origin coordinates
119
offset = [400, 400, 200]
10+
# Machine initial position (in workpiece coordinates!)
11+
zero = [0, 0, 500]
1212
# Maximum feed rate in mm/min
1313
fmax = 10000
1414
# Tool table
@@ -25,37 +25,37 @@ sub_topic = "c-cnc/status/#"
2525

2626
# SI units!
2727
[X]
28-
length = 1 # m
29-
friction = 1000 #
30-
mass = 150 # kg
31-
max_torque = 20 # N m
28+
length = 1 # m
29+
friction = 1000 #
30+
mass = 150 # kg
31+
max_torque = 20 # N m
3232
pitch = 0.01 # m/rev
33-
gravity = 0 # m/s^2
34-
integration_dt = 5 # microseconds
35-
p = 30 # PID parameters
36-
i = 0 # PID parameters
37-
d = 19 # PID parameters
33+
gravity = 0 # m/s^2
34+
integration_dt = 5 # microseconds
35+
p = 1400 # PID parameters
36+
i = 0 # PID parameters
37+
d = 360 # PID parameters
3838

3939
[Y]
40-
length = 1 # m
41-
friction = 1000 #
42-
mass = 150 # kg
43-
max_torque = 20 # N m
40+
length = 1 # m
41+
friction = 1000 #
42+
mass = 150 # kg
43+
max_torque = 20 # N m
4444
pitch = 0.01 # m/rev
45-
gravity = 0 # m/s^2
46-
integration_dt = 5 # microseconds
47-
p = 700 # PID parameters
48-
i = 0 # PID parameters
49-
d = 180 # PID parameters
45+
gravity = 0 # m/s^2
46+
integration_dt = 5 # microseconds
47+
p = 700 # PID parameters
48+
i = 0 # PID parameters
49+
d = 180 # PID parameters
5050

5151
[Z]
52-
length = 1 # m
53-
friction = 1000 #
54-
mass = 100 # kg
55-
max_torque = 15 # N m
52+
length = 1 # m
53+
friction = 1000 #
54+
mass = 100 # kg
55+
max_torque = 15 # N m
5656
pitch = 0.01 # m/rev
57-
gravity = 0 # m/s^2
58-
integration_dt = 5 # microseconds
59-
p = 350 # PID parameters
60-
i = 0 # PID parameters
61-
d = 90 # PID parameters
57+
gravity = 0 # m/s^2
58+
integration_dt = 5 # microseconds
59+
p = 350 # PID parameters
60+
i = 0 # PID parameters
61+
d = 90 # PID parameters

src/axis.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ int main(int argc, char const **argv) {
293293
axis_run(ax);
294294
axis_run(ay);
295295
axis_run(az);
296-
a = az;
296+
a = ax;
297+
fprintf(stderr, "axis %s: p %.3f i %.3f d %.3f\n", a->name, a->p, a->i, a->d);
297298
while (a->time < atof(argv[1])) {
298299
if (a->time < 1) {
299300
axis_set_setpoint(a, 0);

src/fsm.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Functions and types have been generated with prefix "ccnc_"
1717
#include <stdio.h>
1818
#include <termios.h> // setting terminal attributes
1919
#include <unistd.h>
20+
#include <math.h>
21+
#include <sys/param.h>
2022
#include "defines.h"
2123
#include "fsm.h"
2224
#include "block.h"
@@ -201,6 +203,8 @@ ccnc_state_t ccnc_do_idle(ccnc_state_data_t *data) {
201203
// 2. Reset times
202204
data->t_blk = data->t_tot = 0;
203205

206+
machine_sync(data->machine, 1);
207+
204208
switch (next_state) {
205209
case CCNC_NO_CHANGE:
206210
case CCNC_STATE_IDLE:
@@ -425,10 +429,11 @@ ccnc_state_t ccnc_do_rapid_motion(ccnc_state_data_t *data) {
425429
point_z(pos));
426430

427431
// 5. print progress percentage
428-
fprintf(stderr, "\b\b\b\b\b\b\b\b");
429-
fflush(stderr);
430-
fprintf(stderr, "[%5.1f%%]",
431-
(1 - machine_error(data->machine) / block_length(b)) * 100);
432+
// fprintf(stderr, "\b\b\b\b\b\b\b\b");
433+
// fflush(stderr);
434+
fprintf(stderr, "\r[%5.1f%%]", fabs(
435+
1.0 - MIN(machine_error(data->machine) / block_length(b), 1.0)) *
436+
100);
432437

433438
// 6. increment times
434439
data->t_blk += tq;
@@ -573,7 +578,7 @@ void ccnc_begin_rapid(ccnc_state_data_t *data) {
573578
machine_sync(data->machine, 1);
574579

575580
// 4. Print INITIAL value for progress string (8 chars)
576-
fprintf(stderr, "Rapid block length: %f\n", block_length(b));
581+
// fprintf(stderr, "Rapid block length: %f\n", block_length(b));
577582
fprintf(stderr, "[ 0.0%%]");
578583
}
579584

src/machine.c

+5-16
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,18 @@ void machine_print_params(machine_t const *m) {
231231

232232
int machine_connect(machine_t *m, machine_on_message callback) {
233233
assert(m);
234-
int count = 0;
235234
m->mqt = mosquitto_new(NULL, 1, m);
236235
if (!m->mqt) {
237236
perror(BRED"Could not create MQTT"CRESET);
238237
return EXIT_FAILURE;
239238
}
240239
mosquitto_connect_callback_set(m->mqt, on_connect);
241240
mosquitto_message_callback_set(m->mqt, callback ? callback : on_message);
241+
mosquitto_loop_start(m->mqt);
242242
if (mosquitto_connect(m->mqt, m->broker_address, m->broker_port, 10) != MOSQ_ERR_SUCCESS) {
243243
perror(BRED"Invalid broker connection parameters"CRESET);
244244
return EXIT_FAILURE;
245245
}
246-
// wait for the connection to be established
247-
while (m->connecting) {
248-
wprintf("loop: %d\n", mosquitto_loop(m->mqt, -1, 1));
249-
if (++count >= 5) {
250-
eprintf("Could not connect to broker\n");
251-
return EXIT_FAILURE;
252-
}
253-
}
254246
return EXIT_SUCCESS;
255247
}
256248

@@ -269,9 +261,6 @@ int machine_sync(machine_t *m, int rapid) {
269261
perror(BRED"Could not sent message"CRESET);
270262
return EXIT_FAILURE;
271263
}
272-
if(mosquitto_loop(m->mqt, 0, 1) != MOSQ_ERR_SUCCESS) {
273-
perror(BRED"mosquitto_loop error"CRESET);
274-
}
275264
return EXIT_SUCCESS;
276265
}
277266

@@ -282,7 +271,7 @@ int machine_listen_start(machine_t *m) {
282271
return EXIT_FAILURE;
283272
}
284273
m->error = m->max_error * 10.0;
285-
wprintf("Subscribed to topic %s\n", m->sub_topic);
274+
// wprintf("Subscribed to topic %s\n", m->sub_topic);
286275
return EXIT_SUCCESS;
287276
}
288277

@@ -292,16 +281,16 @@ int machine_listen_stop(machine_t *m) {
292281
perror(BRED"Could not unsubscribe"CRESET);
293282
return EXIT_FAILURE;
294283
}
295-
wprintf("Unsubscribed from topic %s\n", m->sub_topic);
284+
// wprintf("Unsubscribed from topic %s\n", m->sub_topic);
296285
return EXIT_SUCCESS;
297286
}
298287

299288
void machine_disconnect(machine_t *m) {
300289
assert(m && m->mqt);
301290
while (mosquitto_want_write(m->mqt)) {
302-
mosquitto_loop(m->mqt, 0, 1);
303291
usleep(10000);
304292
}
293+
mosquitto_loop_stop(m->mqt, 1);
305294
mosquitto_disconnect(m->mqt);
306295
m->connecting = 1;
307296
}
@@ -315,7 +304,7 @@ static void on_connect(struct mosquitto *mqt, void *obj, int rc) {
315304
machine_t *m = (machine_t *)obj;
316305
// check rc: if CONNACK_ACCEPTED, then the connection succeeded
317306
if (rc == CONNACK_ACCEPTED) {
318-
wprintf("-> Connected to %s:%d\n", m->broker_address, m->broker_port);
307+
wprintf("-> Connected to %s:%d\n\r", m->broker_address, m->broker_port);
319308
// subscribe to topic
320309
if (mosquitto_subscribe(mqt, NULL, m->sub_topic, 0) != MOSQ_ERR_SUCCESS) {
321310
perror("Could not subsccribe");

src/main/simulate.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef struct {
2929
struct mosquitto *mqtt;
3030
int connecting;
3131
int rapid;
32+
int program_run;
3233
} sim_t;
3334

3435
// empty signal handler
@@ -43,7 +44,7 @@ sim_t *sim_new(char *ini_path) {
4344
sim_t *sim = malloc(sizeof(sim_t));
4445
memset(sim, 0, sizeof(*sim));
4546
sim->connecting = 1;
46-
sim->rapid = 1;
47+
sim->rapid = 0;
4748

4849
// init axes
4950
sim->ax = axis_new(ini_path, "X");
@@ -140,6 +141,7 @@ static void on_message(struct mosquitto *m, void *obj,
140141
sim_t *sim = (sim_t *)obj;
141142
char *substr = NULL;
142143
data_t x, y, z;
144+
sim->program_run = 1;
143145
if (strcmp(msg->topic, sim->sub_topic) == 0) {
144146
substr = strchr(msg->payload, 'x');
145147
x = atof(substr + 3) / 1000.0;
@@ -239,8 +241,13 @@ int main(int argc, char const *argv[]) {
239241
axis_reset(ax, 0);
240242
axis_reset(ay, 0);
241243
axis_reset(az, 0);
244+
axis_set_setpoint(ax, 0);
245+
axis_set_setpoint(ay, 0);
246+
axis_set_setpoint(az, 0);
242247

243-
#define LOG_HEADER "t qx sx x vx qy sy y vy qz sz z vz\n"
248+
#define LOG_HEADER \
249+
" t qx sx x vx qy sy " \
250+
" y vy qz sz z vz d r\n"
244251
printf(LOG_HEADER);
245252
if (logfile) {
246253
fprintf(logfile, LOG_HEADER);
@@ -262,15 +269,14 @@ int main(int argc, char const *argv[]) {
262269
y = axis_position(ay) * 1000;
263270
z = axis_position(az) * 1000;
264271
delta = sqrt(pow(x - sx, 2) + pow(y - sy, 2) + pow(z - sz, 2));
265-
sprintf(payload, "%f", delta);
266272
printf("\r");
267-
printf("%f " RED "%9.3f %9.3f %9.3f %9.3f " GRN
273+
printf("%9.4f " RED "%9.3f %9.3f %9.3f %9.3f " GRN
268274
"%9.3f %9.3f %9.3f %9.3f " BLU "%9.3f %9.3f %9.3f %9.3f" YEL
269275
" %9.3f" CRESET " %c",
270276
axis_time(ax), axis_torque(ax), sx, x, axis_speed(ax),
271277
axis_torque(ay), sy, y, axis_speed(ay), axis_torque(az), sz, z,
272278
axis_speed(az), delta, sim->rapid ? 'R' : 'I');
273-
if (logfile) {
279+
if (logfile && sim->program_run) {
274280
fprintf(logfile,
275281
"%f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f "
276282
"%9.3f %9.3f %9.3f %c\n",
@@ -279,7 +285,8 @@ int main(int argc, char const *argv[]) {
279285
axis_speed(az), delta, sim->rapid ? 'R' : 'I');
280286
}
281287
fflush(stdout);
282-
if (sim->rapid) {
288+
if (sim->rapid && sim->program_run) {
289+
sprintf(payload, "%f", delta);
283290
mosquitto_publish(sim->mqtt, NULL, sim->pub_topic_err, strlen(payload),
284291
payload, 0, 0);
285292
sprintf(payload, "%f,%f,%f", x, y, z);

0 commit comments

Comments
 (0)