/
Example.c
executable file
·102 lines (98 loc) · 3.43 KB
/
Example.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
/*
* FPGA Interface C API example for GCC for computers running Linux.
*
* NOTE: In order to run this example, you must compile a LabVIEW FPGA bitfile
* and generate a C API for it. For more information about using this
* example, refer to the Examples topic of the FPGA Interface C API Help,
* located under
* Start>>All Programs>>National Instruments>>FPGA Interface C API.
*/
#include "NiFpga_Math_Pid.h"
#include <stdio.h>
#include <stdlib.h>
#define TICKS_PER_DEGREE 1024/360
// ticks per revolution divided by 360 to get ticks per degree
#define ACCURACY 10000
double tick_degree(int32_t tick)
{
double degree;
degree = tick / TICKS_PER_DEGREE;
return degree;
}
int32_t degree_tick(double degree)
{
int32_t tick;
tick = degree * TICKS_PER_DEGREE;
return tick;
}
int main()
{
double desired_angle;
int32_t desired_tick;
int32_t Kd = .5 * ACCURACY;
int32_t Kp = 5 * ACCURACY;
int32_t Ki = .0001 * ACCURACY;
int32_t error=0xFFFF;
/* must be called before any other calls */
printf("Initializing...\n");
NiFpga_Status status = NiFpga_Initialize();
if (NiFpga_IsNotError(status))
{
NiFpga_Session session;
/* opens a session, downloads the bitstream, and runs the FPGA */
printf("Opening a session...\n");
NiFpga_MergeStatus(&status,
NiFpga_Open(NiFpga_Math_Pid_Bitfile, NiFpga_Math_Pid_Signature,
"rio://146.6.84.251/RIO0",
NiFpga_OpenAttribute_NoRun,&session));
if (NiFpga_IsNotError(status))
{
/* run the FPGA application */
printf("Running the FPGA...\n");
NiFpga_MergeStatus(&status, NiFpga_Run(session, 0));
/********************************************************************/
/* Code goes here */
NiFpga_MergeStatus(&status,
NiFpga_WriteBool(session,NiFpga_Encoder_Reset,0));
NiFpga_MergeStatus(&status,
NiFpga_WriteI32(session,NiFpga_Kd,Kd));
NiFpga_MergeStatus(&status,
NiFpga_WriteI32(session,NiFpga_Kp,Kp));
NiFpga_MergeStatus(&status,
NiFpga_WriteI32(session,NiFpga_Ki,Ki));
NiFpga_MergeStatus(&status,
NiFpga_WriteI32(session,NiFpga_Accuracy,ACCURACY));
printf("Enter Desired(in degrees): ");
scanf("%lf",&desired_angle);
desired_tick = degree_tick(desired_angle);
printf("Desired(in ticks) %d\n", desired_tick);
NiFpga_MergeStatus(&status,
NiFpga_WriteI32(session,NiFpga_Desired,desired_tick));
printf("Entering loop\n");
while(error != 0) {
NiFpga_MergeStatus(&status,
NiFpga_ReadI32(session,NiFpga_RealError,&error)); printf("Error:%d\n", error);
}
/********************************************************************/
/* Stopping */
printf("Press <Enter> to stop and quit...");
getchar();
/* stop the FPGA loops */
printf("Stopping the FPGA...\n");
/* close the session now that we're done */
printf("Closing the session...\n");
NiFpga_MergeStatus(&status, NiFpga_Close(session, 0));
}
/* must be called after all other calls */
printf("Finalizing...\n");
NiFpga_MergeStatus(&status, NiFpga_Finalize());
}
/* check if anything went wrong */
if (NiFpga_IsError(status))
{
printf("Error %d!\n", status);
printf("Press <Enter> to quit...\n");
getchar();
}
return status;
}