Repository navigation
Expand file tree
/
Copy pathtest.c
More file actions
193 lines (151 loc) · 4.63 KB
/
Copy pathtest.c
File metadata and controls
193 lines (151 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/mman.h>
#include <sched.h>
#include <time.h>
#include <math.h>
#include "shm_data.h"
#include "shm.h"
#define NSEC_PER_SEC 1000000000
pthread_t RT_thread;
volatile sig_atomic_t done = 0;
void intHandler(int sig)
{
if (sig == SIGINT)
{
done = 1;
printf("User requests termination.\n");
}
}
/* Add ns to timespec */
void add_timespec(struct timespec *ts, int64_t addtime) {
int64_t sec, nsec;
/* Seperate elapsed time in parts */
nsec = addtime % NSEC_PER_SEC; /* Elapsed time in whole seconds */
sec = (addtime - nsec) / NSEC_PER_SEC; /* Rest of elapsed time in ns */
ts->tv_sec += sec;
ts->tv_nsec += nsec;
/* nsec is always less than i million */
if ( ts->tv_nsec > NSEC_PER_SEC ) {
nsec = ts->tv_nsec % NSEC_PER_SEC;
ts->tv_sec += (ts->tv_nsec - nsec) / NSEC_PER_SEC;
ts->tv_nsec = nsec;
}
}
int test(void *ptr) {
/* local variables */
void *p; /* intermediate pointer */
// int err; /* error number */
long int i = 0; /* loop iterations */
double pos = 0.0;
int data_size = 10000;
double *data;
double *time;
double *time_pre;
double *time_diff;
data = (double *)malloc(data_size * sizeof(double));
time = (double *)malloc(data_size * sizeof(double));
time_pre = (double *)malloc(data_size * sizeof(double));
time_diff = (double *)malloc(data_size * sizeof(double));
data[0] = time[0] = time_pre[0] = time_diff[0] = 0.0;
/* instanciate input-output data varibles */
//shared_in_t *shared_in;
//shared_out_t *shared_out;
Rdda *rdda;
rdda = initRdda();
for (int i=0; i<2; i++) {
rdda->motor[i].motorIn.act_pos = 0.0;
rdda->motor[i].motorIn.act_vel = 0.0;
rdda->motor[i].rosOut.pos_ref = 0.0;
rdda->motor[i].rosOut.stiffness = 0.0;
}
/* timebase */
struct timespec ts, tleft;
int ht, toff;
int64_t cycletime;
double current_time;
clock_gettime(CLOCK_MONOTONIC, &ts);
ht = (ts.tv_nsec / 1000000) + 1; /* round to nearest ms */
ts.tv_nsec = ht * 1000000;
cycletime = *(int*)ptr * 1000; /* cycletime in ns */
toff = 0;
/* measure time interval */
struct timespec start, end;
double time_taken = 0.0, time_left = 0.0;
/* run in shared memory */
//while(!done && i<=data_size) {
while(!done) {
/* timer */
/* record current time */
// current_time = (double)(ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec) / (NSEC_PER_SEC);
/* calculate next cycle start */
// add_timespec(&ts, cycletime + toff);
/* wait to cycle start */
// clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &tleft);
clock_gettime(CLOCK_MONOTONIC, &start);
// data[i] = pos;
// time_pre[i] = time[i-1];
// time[i] = current_time;
// time_diff[i] = time[i] - time_pre[i];
// printf("current_time: %lf, pos: %lf, cycle: %ld\r", time[i], data[i], i);
mutex_lock(&rdda->mutex);
rdda->ts.nsec = ts.tv_nsec;
rdda->ts.sec = ts.tv_sec;
rdda->motor[0].motorIn.act_pos = 3.14159;
// printf("pos_ref: [%lf, %lf], vel_sat: [%lf, %lf], tau_sat: [%lf, %lf], stiff: [%lf, %lf]\r",
// rdda->motor[0].rosOut.pos_ref, rdda->motor[1].rosOut.pos_ref,
// rdda->motor[0].rosOut.vel_sat, rdda->motor[1].rosOut.vel_sat,
// rdda->motor[0].rosOut.tau_sat, rdda->motor[1].rosOut.tau_sat,
// rdda->motor[0].rosOut.stiffness, rdda->motor[1].rosOut.stiffness);
mutex_unlock(&rdda->mutex);
//i++;
clock_gettime(CLOCK_MONOTONIC, &end);
time_taken = ((double)(end.tv_sec-start.tv_sec)*1000000 + (double)(end.tv_nsec-start.tv_nsec)/1000);
time_left = (double)(500-time_taken);
printf("taken_time: %lf, left_time: %lf\r", time_taken, time_left);
}
/* write data to a file */
// FILE *fptr = fopen("shm.data", "w");
// for (long int j = 0; j<data_size; j++) {
// if (j>i) time[j] = data[j] = 0;
// fprintf(fptr, "%+lf, %+lf, %+lf, %+lf\n", time[j], time_pre[j], time_diff[j], data[j]);
// }
// fclose(fptr);
free(data);
free(time);
free(time_pre);
free(time_diff);
return 0;
}
int main(int argc, char *argv[])
{
signal(SIGINT, intHandler);
int ctime = 1000;
struct sched_param param;
int policy = SCHED_FIFO;
printf("ROS Interface Test\n");
if (argc > 0) {
/* Create RT thread */
pthread_create(&RT_thread, NULL, (void*) &test, (void*) &ctime);
/* Scheduler */
memset(¶m, 0, sizeof(param));
param.sched_priority = 40;
pthread_setschedparam(RT_thread, policy, ¶m);
/* Core-Iso */
cpu_set_t CPU3;
CPU_ZERO(&CPU3);
CPU_SET(3, &CPU3);
pthread_setaffinity_np(RT_thread, sizeof(CPU3), &CPU3);
/* wait until sub-thread is finished */
pthread_join(RT_thread, NULL);
}
return 0;
}