-
Notifications
You must be signed in to change notification settings - Fork 0
/
pru_pwm.c
284 lines (230 loc) · 7.02 KB
/
pru_pwm.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#define PRU_NUM 1
#define PRU_ADDR 0x4A300000
#define SHAREDRAM_OFFSET 0x00010000
// Counts per revolution, rising and falling edge.
#define CPR 576
struct pid_data {
/* PID tunings */
float kp, ki, kd;
/* PID controls */
float setpoint;
float e;
float input, output, last_output;
float min_output, max_output;
};
struct pwm {
FILE *pwm,
*duty,
*period,
*run;
};
void update_pid(volatile struct pid_data*);
void set_pwm(struct pwm*);
void close_pwm(struct pwm*);
void update_pwm_period(struct pwm*, int);
void update_pwm_duty(struct pwm*, int);
int main(void) {
int fd = open("/dev/mem", O_RDWR | O_SYNC);
ulong* imp = (ulong*) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PRU_ADDR+SHAREDRAM_OFFSET);
unsigned int impulses = 0;
unsigned int status;
tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
prussdrv_init();
printf("prussdrv initialized\n");
status = prussdrv_open(PRU_EVTOUT_0);
if (status) {
printf("prussdrv_open open failed!\n");
return (status);
}
prussdrv_pruintc_init(&pruss_intc_initdata);
printf("Interrupt initialized\n");
status = prussdrv_exec_program(PRU_NUM, "./pru_pwm.bin");
if (status) {
printf("prussdrv_exec_program failed!\n");
return (status);
}
// Init PID.
struct pid_data motor1PID;
motor1PID.kp = 1;
motor1PID.ki = 1/8;
motor1PID.kd = 0.125;
motor1PID.min_output = -100;
motor1PID.max_output = 100;
motor1PID.setpoint = -1;
// Init PWM.
struct pwm pwmData;
set_pwm(&pwmData);
update_pwm_period(&pwmData, 50000);
update_pwm_duty(&pwmData, 50000 - 10000);
unsigned int now_ms, last_ms, delta_ms;
float current_pos, last_pos, delta_pos;
struct timespec now;
FILE* inA1 = fopen("/sys/class/gpio/gpio47/direction", "w");
FILE* inB1 = fopen("/sys/class/gpio/gpio27/direction", "w");
int inA1v = 1;
int writeStatus = 0;
fseek(inA1, 0, SEEK_SET);
fprintf(inA1, "%s", "low");
writeStatus = fflush(inA1);
printf("Set inA1: %i\n", writeStatus);
fseek(inB1, 0, SEEK_SET);
fprintf(inB1, "%s", "high");
writeStatus = fflush(inB1);
printf("Set inB1: %i\n", writeStatus);
while (1) {
// prussdrv_pru_wait_event(PRU_EVTOUT_0);
// prussdrv_pru_clear_event(PRU_EVTOUT_0, PRU0_ARM_INTERRUPT);
// printf("Imp: %i\n", (int) imp[0]);
clock_gettime(CLOCK_REALTIME, &now);
impulses = imp[0];
// now_ms = (now.tv_sec * 1000 + (now.tv_nsec + 500000) / 100000); //convert to milliseconds
// delta_ms = now_ms - last_ms;
current_pos = (float) impulses / 576;
motor1PID.input = current_pos;
update_pid(&motor1PID);
update_pwm_duty(&pwmData, 50000 - (motor1PID.output*500));
if (motor1PID.output < 0 && inA1v != 1) {
inA1v = 1;
fseek(inA1, 0, SEEK_SET);
fprintf(inA1, "%s", "high");
writeStatus = fflush(inA1);
fseek(inB1, 0, SEEK_SET);
fprintf(inB1, "%s", "low");
writeStatus = fflush(inB1);
} else if (motor1PID.output > 0 && inA1v != 0) {
inA1v = 0;
fseek(inA1, 0, SEEK_SET);
fprintf(inA1, "%s", "low");
writeStatus = fflush(inA1);
fseek(inB1, 0, SEEK_SET);
fprintf(inB1, "%s", "high");
//writeStatus = fflush(inB1);
}
last_pos = current_pos;
last_ms = now_ms;
printf("%i, %f, %i\n", impulses, motor1PID.output, inA1);
//if (impulses > CPR) {
// break;
//}
}
update_pwm_duty(&pwmData, 50000);
close_pwm(&pwmData);
printf("Finished!\n");
prussdrv_pru_clear_event(PRU_EVTOUT_0, PRU0_ARM_INTERRUPT);
return 0;
}
/**
* Updates pwm.
*
* @param pid
*/
void update_pid(volatile struct pid_data* pid) {
float p_f, d_f;
float output_f, output;
/* Calculate error */
float error = (pid->input - pid->setpoint);
// printf("\nErr %f\n", error);
/* Calculate P term */
p_f = pid->kp * error;
/* Integrate I term */
pid->e += (pid->ki * error); // >> SHIFT;
/* Calculate D term */
d_f = pid->kd * (pid->output - pid->last_output);
/* Sum PID output */
output_f = p_f + pid->e + d_f;
output = output_f; // >> SHIFT;
printf("\nOut %f\n", output);
/* Set output_f, check min/max output */
if (output < pid->min_output) output = pid->min_output;
if (output > pid->max_output) output = pid->max_output;
pid->last_output = pid->output;
pid->output = pid->max_output - output;
}
/**
*
* echo /sys/devices/ocp.3/pwm_test_P8_13.* /
*
* @param pwmData
*/
void set_pwm(struct pwm* pwmData)
{
pwmData->pwm = fopen("/sys/devices/bone_capemgr.9/slots", "w");
if (pwmData->pwm == NULL)
printf("Problem with file for pwm.\n");
pwmData->period = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/period", "w");
if (pwmData->period == NULL)
printf("Problem with file for pwm-period.\n");
pwmData->duty = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/duty", "w");
if (pwmData->duty == NULL)
printf("Problem with file for pwm-duty.\n");
pwmData->run = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/run", "w");
if (pwmData->run == NULL)
printf("Problem with file for pwm-run.\n");
int writeStatus = 0;
//
fseek(pwmData->pwm, 0, SEEK_SET);
fprintf(pwmData->pwm, "am33xx_pwm");
writeStatus = fflush(pwmData->pwm);
printf("Status am33xx_pwm: %i\n", writeStatus);
fprintf(pwmData->pwm, "bone_pwm_P8_13");
writeStatus = fflush(pwmData->pwm);
printf("Status bone_pwm_P8_13: %i\n", writeStatus);
//
fseek(pwmData->run, 0, SEEK_SET);
fprintf(pwmData->run, "%d", 0);
writeStatus = fflush(pwmData->run);
printf("Status run0: %i\n", writeStatus);
fseek(pwmData->run, 0, SEEK_SET);
fprintf(pwmData->run, "%d", 1);
writeStatus = fflush(pwmData->run);
printf("Status run1: %i\n", writeStatus);
}
/**
*
* @param pwm pwmData
*/
void close_pwm(struct pwm* pwmData)
{
fclose(pwmData->pwm);
fclose(pwmData->duty);
fclose(pwmData->period);
fclose(pwmData->run);
}
/**
* Updates pwm.
*
* @param pwm pwmData
* @param int period 200000000
*
* @return void
*/
void update_pwm_period(struct pwm* pwmData, int iPeriod)
{
int writeStatus = 0;
fseek(pwmData->period, 0, SEEK_SET);
fprintf(pwmData->period, "%d", iPeriod);
writeStatus = fflush(pwmData->period);
// printf("Period status: %i\n", writeStatus);
}
/**
* Updates pwm.
*
* @param pwm pwmData
* @param int iDuty 100000000
*
* @return void
*/
void update_pwm_duty(struct pwm* pwmData, int iDuty)
{
int writeStatus = 0;
fseek(pwmData->duty, 0, SEEK_SET);
fprintf(pwmData->duty, "%d", iDuty);
writeStatus = fflush(pwmData->duty);
// printf("Duty status %i\n", writeStatus);
}