This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunBMChex111.c
212 lines (176 loc) · 6.31 KB
/
runBMChex111.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
#include <BMCApi.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <ImageStruct.h> // cacao data structure definition
#include <ImageStreamIO.h> // function ImageStreamIO_read_sharedmem_image_toIMAGE()
#include <unistd.h>
#define DMVOLT_FILENAME "dmvolt"
#define DMPTT_FILENAME "dmptt"
typedef int bool_t;
#define TRUE 1
#define FALSE 0
struct timespec tnow;
double tnowdouble;
double tlastupdatedouble;
double dt_update; // time since last update
double dt_update_lim = 360000000000000.0; // if no command is received during this time, set DM to zero V [sec]
/*==========================================================================*/
static int loop_abort = 0; // termination flag
// termination function for SIGINT callback
static void endme(int dummy)
{
loop_abort = 1;
}
void Sleep_us(double us)
{
usleep((unsigned int)us);
}
void ShowHelp(void)
{
printf("Boston Micromachines DM tester.\n");
printf(" DM-SDK Version %s\n", BMCVersionString());
printf("Test 1: Piston mirror between two values.\n");
printf("Test 2: Poke each actuator to half amplitude.\n");
printf("Options are:\n");
printf(" -s xxxxxxx Device serial number. If this\n");
printf(" option is not used, you will be\n");
printf(" prompted to enter the SN.\n");
printf(" -c x Run test continuously with x\n");
printf(" millisecond delay between commands.\n");
printf(" -a Exit when done, don't wait for input.\n\n");
printf(" -h Display this message and exit.\n\n");
}
/*!
* \brief poke_pcie Poke actuators one by one using BMCSetSingle()
* \param hdm DM handle
* \param continuous If true, don't wait for user input.
* \param delay Delay between actuators in continuous mode.
* \return Error code
*/
int main(int argc, char *argv[])
{
DM hdm;
BMCRC rv;
double *test_array1;
double *test_array2;
double *bmc_array;
double *bmc_voltarray;
IMAGE *SMdmvolt;
IMAGE *SMdmptt;
/////// INIT SHARED MEM STUFF
long naxis; // number of axis
uint8_t atype; // data type
uint32_t *imsize; // image size
int shared; // 1 if image in shared memory
int NBkw; // number of keywords supported
int CBsize;
uint32_t *map_lut;
char serial_number[BMC_SERIAL_NUMBER_LEN + 1];
char app_name[BMC_MAX_PATH] = "";
int k, i = 0;
long ii;
int select = 0;
int err_count = 0;
int pistons = 10;
int delay_us = 500;
char ch = '\0';
bool_t continuous = FALSE;
bool_t auto_exit = FALSE;
bool_t ask_SN = TRUE;
long framecnt;
long cnt0;
long cntloop;
int activate_MEMS;
// Parse options
snprintf(app_name, sizeof(app_name), "%s", argv[0]);
// register interrupt signal to terminate the main loop
signal(SIGINT, endme);
argc--;
ShowHelp();
// Add Static Serial Number
strcpy(serial_number, "32AW005#010");
// strcpy(serial_number, "32AW038#027");
// Open driver
memset(&hdm, 0, sizeof(hdm));
rv = BMCOpen(&hdm, serial_number);
unsigned int numSegments = hdm.ActCount / 3;
if (rv)
{
printf("Error %d opening the driver type %u.\n", rv, (unsigned int)hdm.Driver_Type);
printf("%s\n\n", BMCErrorString(rv));
}
rv = BMCLoadCalibrationFile(&hdm, "/opt/Boston Micromachines/Calibration/LUT_32AW005#010.mat");
printf("Boston Micromachines C API Example.\n");
printf("Opened Device %d with %d actuators.\n", hdm.DevId, hdm.ActCount);
// Allocate and initialize control arrays.
map_lut = (uint32_t *)malloc(sizeof(uint32_t) * MAX_DM_SIZE);
bmc_array = (double *)calloc(hdm.ActCount, sizeof(double));
bmc_voltarray = (double *)calloc(hdm.ActCount, sizeof(double));
for (k = 0; k < (int)hdm.ActCount; k++)
{
map_lut[k] = 0;
}
// Open default actuator map from disk
rv = BMCLoadMap(&hdm, NULL, map_lut);
double temp[hdm.ActCount];
SMdmvolt = (IMAGE *)malloc(sizeof(IMAGE));
SMdmptt = (IMAGE *)malloc(sizeof(IMAGE));
ImageStreamIO_read_sharedmem_image_toIMAGE(DMVOLT_FILENAME, SMdmvolt);
ImageStreamIO_read_sharedmem_image_toIMAGE(DMPTT_FILENAME, SMdmptt);
memset(SMdmvolt->array.D, 0, hdm.ActCount * sizeof(double));
memset(SMdmptt->array.D, 0, hdm.ActCount * sizeof(double));
int semID_dmvolt = ImageStreamIO_getsemwaitindex(SMdmvolt, -1);
int semID_dmptt = ImageStreamIO_getsemwaitindex(SMdmptt, -1);
// MAIN LOOP
printf("ENTERING LOOP\n");
while (!loop_abort && (SMdmvolt[0].md[0].status < 100) && (SMdmptt[0].md[0].status < 100))
{
if (!sem_trywait(SMdmvolt->semptr[semID_dmvolt]))
{
// Post an actuator command !
rv = BMCSetArray(&hdm, SMdmvolt->array.D, map_lut);
clock_gettime(CLOCK_REALTIME, &tnow);
tlastupdatedouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
}
else if (!sem_trywait(SMdmptt->semptr[semID_dmvolt]))
{
// Post a PTT command !
for (k = 0; k < numSegments; k++)
{
rv = BMCSetSegment(&hdm, k,
SMdmptt->array.D[3 * k],
SMdmptt->array.D[3 * k + 1],
SMdmptt->array.D[3 * k + 2],
TRUE, k == numSegments - 1);
}
clock_gettime(CLOCK_REALTIME, &tnow);
tlastupdatedouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
}
else
{
// Do not post a command
usleep(10); // 10 us
clock_gettime(CLOCK_REALTIME, &tnow);
tnowdouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
dt_update = tnowdouble - tlastupdatedouble;
if (dt_update > dt_update_lim)
{
// set DM to zero
printf("DM going to sleep ... \n");
// Send zero volts??
BMCClearArray(&hdm);
tlastupdatedouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
}
}
}
printf("EXITING MAIN LOOP\n");
// Clear array
rv = BMCClearArray(&hdm);
if (rv)
printf("Error %d clearing voltages.\n", rv);
rv = BMCClose(&hdm);
return 0;
}