-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphidget.c
executable file
·196 lines (143 loc) · 4.78 KB
/
phidget.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <phidget21.h>
#include "global.h"
#include "phidget.h"
int CCONV AttachHandler(CPhidgetHandle phid, void *userptr)
{
CPhidgetBridgeHandle bridge = (CPhidgetBridgeHandle)phid;
CPhidgetBridge_setEnabled(bridge, 0, PTRUE);
CPhidgetBridge_setEnabled(bridge, 1, PTRUE);
CPhidgetBridge_setEnabled(bridge, 2, PFALSE);
CPhidgetBridge_setEnabled(bridge, 3, PFALSE);
CPhidgetBridge_setGain(bridge, 0, PHIDGET_BRIDGE_GAIN_128);
CPhidgetBridge_setGain(bridge, 1, PHIDGET_BRIDGE_GAIN_16);
CPhidgetBridge_setGain(bridge, 2, PHIDGET_BRIDGE_GAIN_32);
CPhidgetBridge_setGain(bridge, 3, PHIDGET_BRIDGE_GAIN_64);
CPhidgetBridge_setDataRate(bridge, 1000);
strcpy(MsgPhidgets, "Attach handler ran!"); // set status MSG
return 0;
}
int CCONV DetachHandler(CPhidgetHandle phid, void *userptr)
{
//MsgPhidgets = "Detach handler ran!\n";
return 0;
}
int CCONV ErrorHandler(CPhidgetHandle phid, void *userptr, int ErrorCode, const char *errorStr)
{
//printf("Error event: %s\n",errorStr);
strcpy(MsgPhidgets, "Error event Phidget"); // Set status MSG
return 0;
}
void display_generic_properties(CPhidgetHandle phid)
{
int sernum, version;
const char *deviceptr;
CPhidget_getDeviceType(phid, &deviceptr);
CPhidget_getSerialNumber(phid, &sernum);
CPhidget_getDeviceVersion(phid, &version);
//printf("%s\n", deviceptr);
//printf("Version: %8d SerialNumber: %10d\n", version, sernum);
return;
}
int CCONV UpdateAngle(CPhidgetBridgeHandle phid, void *userPtr, int index, double val)
{
CPhidgetBridgeHandle bridge = (CPhidgetBridgeHandle)phid;
double f, ms;
int i;
// Force X
if(index == 0){
// No filter
if(dataFilter == 0){
forceX = val;
}
// Lowpass moving avarage filter
else if(dataFilter == 1){
forceX = (val+forceX)/2;
}
// Lowpass complementary filter
else if(dataFilter == 2){
forceX = val*0.1+forceX*0.9;
}
}
//Force Y
if(index == 1){
// No filter
if(dataFilter == 0){
forceY = val;
}
// Lowpass moving avarage filter
else if(dataFilter == 1){
forceY = (val+forceY)/2;
}
// Lowpass complementary filter
else if(dataFilter == 2){
forceY = val*0.1+forceY*0.9;
}
}
// store to struct
loadcellX.input = forceX;
loadcellY.input = forceY;
loadcellX.force = loadcellX.K*(forceX-loadcellX.offset);
loadcellY.force = loadcellY.K*(forceY-loadcellY.offset);
// Calc circular
r = sqrt(pow(forceX,2)+pow(forceY,2));
phi = atan(forceY/forceX);
//printf("Data Event ForceX: %lf\t ForceY: %lf\t Total force: %lf\n",forceX,forceY,forceX+forceY);
/*if(val < 0)
printf("Data Event (%d) - -0x%06x\n",index,(int)-val);
else
printf("Data Event (%d) - +0x%06x\n",index,(int)val);*/
return 0;
}
void GetAngle(void)
{
const char *err;
int result;
CPhidgetBridgeHandle bridge;
//CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
//printf("Creating messurement thread\n");
strcpy(MsgPhidgets, "Creating messurement thread"); // set status MSG
// Initial condition
forceX = 0;
forceY = 0;
loadcellX.offset = -0.05;
loadcellX.K = 1;
loadcellY.offset = -0.05;
loadcellY.K = 1;
CPhidgetBridge_create(&bridge);
CPhidget_set_OnAttach_Handler((CPhidgetHandle)bridge, AttachHandler, NULL);
CPhidget_set_OnDetach_Handler((CPhidgetHandle)bridge, DetachHandler, NULL);
CPhidget_set_OnError_Handler((CPhidgetHandle)bridge, ErrorHandler, NULL);
CPhidgetBridge_set_OnBridgeData_Handler(bridge, UpdateAngle, NULL);
CPhidget_open((CPhidgetHandle)bridge, -1);
//Wait for 10 seconds, otherwise exit
if(result = CPhidget_waitForAttachment((CPhidgetHandle)bridge, dataRate))
{
CPhidget_getErrorDescription(result, &err);
//printf("Problem waiting for attachment: %s\n", err);
strcpy(MsgPhidgets, "Problem waiting for attachment"); // Set status MSG
return;
}
// display_generic_properties((CPhidgetHandle)bridge);
//Wait for stop
while(stop == 0){
// wait for stop signal
}
//printf("Closing...\n");
strcpy(MsgPhidgets,"Closing..."); // Set status MSG
CPhidget_close((CPhidgetHandle)bridge);
CPhidget_delete((CPhidgetHandle)bridge);
return;
}
void Calibration(double noLoadX, double noLoadY, double loadX, double loadY, double load){
// Record the output of the load cell at rest on a flat surface with no force on it. The mv/V output measured by the PhidgetBridge is the offset.
loadcellX.offset = noLoadX;
loadcellY.offset = noLoadY;
// Once you’ve found the offset, measure something with a known weight and solve the equation for K. You can also calibrate the load cell at multiple known weights and use these points to model a linear function.
// Expected Force or Weight = K * (Measured mV/V - Offset)
loadcellX.K = load/(loadX-loadcellX.offset);
loadcellY.K = load/(loadY-loadcellY.offset);
}