-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_csv.c
More file actions
178 lines (152 loc) · 5.67 KB
/
Copy pathread_csv.c
File metadata and controls
178 lines (152 loc) · 5.67 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
/*
* =============================================================================
* File : read_csv.c
* Author : Leandro Martins dos Santos
* Created on : 2024-07-04
* Last Update : 2025-11-04
* Version : 1.0
* Description : CSV parsing and trajectory data preparation for Power PMAC.
* This module reads CSV files containing motion profiles and
* prepares them for execution via shared memory or buffer transfer.
*
* Dependencies: stdio.h, stdlib.h, string.h
* gplib.h (Power PMAC library interface)
* pp_proj.h (project-specific definitions and shared memory access)
*
* Compiler : Power PMAC IDE (Visual Studio-based) or GCC (Linux)
* Target : Power PMAC CPU (Linux/Xenomai RT kernel)
*
* License : Apache-2.0
*
* =============================================================================
*/
#include <gplib.h>
#include <stdlib.h>
#include <errno.h>
#define _PPScriptMode_ // for enum mode, replace this with #define _EnumMode_
#include "../../Include/pp_proj.h"
#include "read_csv.h"
int write_PVT(FILE *file, int bufferNum)
{
// USHM PVT buffers
int *pushm_time;
int *pushm_user;
double *pushm_positions[NUM_AXES];
double *pushm_velocities[NUM_AXES];
int line_count = 0;
// Initialize buffers addresses
pushm_time = (int *) pushm + USHM_INT_BASE_IDX + (bufferNum * USHM_BUFF_OFFSET_INT_IDX);
pushm_user = (int *) pushm + USHM_INT_BASE_IDX + (bufferNum * USHM_BUFF_OFFSET_INT_IDX)+1;
int axis;
for (axis = 0; axis < NUM_AXES; axis++) {
pushm_positions[axis] = (double *) pushm + (USHM_DOUBLE_BASE_IDX + axis) + (bufferNum * USHM_BUFF_OFFSET_DOUBLE_IDX);
}
for (axis = 0; axis < NUM_AXES; axis++) {
pushm_velocities[axis] = (double *) pushm + (USHM_DOUBLE_BASE_IDX+NUM_AXES + axis) + (bufferNum * USHM_BUFF_OFFSET_DOUBLE_IDX);
}
char line[MAX_LINE_SIZE];
// Read the first line
if (fgets(line, sizeof(line), file) != NULL) {
char *token = strtok(line, ",");
while (token != NULL) {
// Store header Data
// ...
token = strtok(NULL, ",");
}
}
while (fgets(line, sizeof(line), file) && line_count < USHM_BUFF_SIZE) {
char *field = strtok(line, ",");
*pushm_time = atoi(field);
field = strtok(NULL, ",");
pushm_time += USHM_LINE_OFFSET_INT_IDX;
*pushm_user = atoi(field);
field = strtok(NULL, ",");
pushm_user += USHM_LINE_OFFSET_INT_IDX; // motot/axis wise
// pushm_user += (NUM_AXES+1)*2; //point-wise
for (axis = 0; axis < NUM_AXES; axis++) {
*pushm_positions[axis] = atof(field);
field = strtok(NULL, ",");
pushm_positions[axis] += USHM_LINE_OFFSET_DOUBLE_IDX; // motor/axis wise
// pushm_positions[axis] += (NUM_AXES+1); //point-wise
}
for (axis = 0; axis < NUM_AXES; axis++) {
*pushm_velocities[axis] = atof(field);
field = strtok(NULL, ",");
pushm_velocities[axis] += USHM_LINE_OFFSET_DOUBLE_IDX;
}
line_count++;
}
return 0;
}
int write_positions(FILE *file, int bufferNum, int *line_count)
{
// USHM Motor positions buffers
int *pushm_user;
double *pushm_positions[NUM_AXES];
// Initialize buffers addresses
pushm_user = (int *) pushm + USHM_INT_BASE_IDX + (bufferNum * USHM_BUFF_OFFSET_INT_IDX)+1;
int axis;
for (axis = 0; axis < NUM_AXES; axis++) {
pushm_positions[axis] = (double *) pushm + (USHM_DOUBLE_BASE_IDX + axis) + (bufferNum * USHM_BUFF_OFFSET_DOUBLE_IDX);
}
char line[MAX_LINE_SIZE];
// Read the first line
if (fgets(line, sizeof(line), file) != NULL) {
char *token = strtok(line, ",");
while (token != NULL) {
// Store header Data
// ...
token = strtok(NULL, ",");
}
}
while (fgets(line, sizeof(line), file) && (*line_count) < USHM_BUFF_SIZE) {
char *field = strtok(line, ",");
*pushm_user = atoi(field);
field = strtok(NULL, ",");
// pushm_user += USHM_LINE_OFFSET_INT_IDX; // motot/axis wise
pushm_user += (NUM_AXES+1)*2; //point-wise
for (axis = 0; axis < NUM_AXES; axis++) {
*pushm_positions[axis] = atof(field);
field = strtok(NULL, ",");
// pushm_positions[axis] += USHM_LINE_OFFSET_DOUBLE_IDX; // motor/axis wise
pushm_positions[axis] += (NUM_AXES+1); //point-wise
}
(*line_count)++;
}
return 0;
}
int read_csv(char *filename, int profileType, int bufferNum)
{
FILE *file = fopen(filename, "r");
int line_count = 0;
if (file == NULL) {
printf("Could not open file %s", filename);
return 1;
}
if (profileType == 0) {
write_PVT(file, bufferNum);
}
else if (profileType == 1) {
write_positions(file, bufferNum,&line_count);
}
SetPtrVar(BufferFill_A+bufferNum, line_count);
// printf("lines: %d\n",line_count);
fclose(file);
return 0;
}
int main(int argc, char *argv[])
{
InitLibrary(); // Required for accessing Power PMAC library
// double exec_time = GetCPUClock(); // Evaluation purposes only
// TODO: Add argc checks, and set defaults values for profileType and bufferNum
char *filename = argv[1];
uint8_t profileType = atoi(argv[2]); // 0: PVT, 1:Motors positions + User Commands
uint8_t bufferNum = atoi(argv[3]); // 0:BufferA, 1:BufferB, [2:BufferC]
// int line_count;
read_csv(filename, profileType, bufferNum);
// exec_time = GetCPUClock()-exec_time;
// printf("Lines number: %d\n", line_count);
// printf("Execution time: %f us\n",exec_time);
CloseLibrary();
return 0;
}