-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphidget_connection.c
More file actions
216 lines (197 loc) · 6.1 KB
/
phidget_connection.c
File metadata and controls
216 lines (197 loc) · 6.1 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
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
/**************************************************
* FILENAME: phidget_connection.c
*
* DESCRIPTION:
* Implementation of several functions required to communicate with phidgets.
* The functions handles creation, reading and setting values, and closing the
* connections. External variables referencing the components are kept to increase
* encapsulation.
*
* PUBLIC FUNCTIONS:
* int connect_phidgets(void)
* int get_sensor_value(void)
* void set_servo_position(double)
* void close_connections(void)
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
#include <phidget21.h>
#include <stdio.h>
#include "headers/responsive_analog_read.h"
// IMPORTANT: set this to the id of your sensor on the interface kit
static const int SENSOR_ID = 2;
// handles for identifying the phidgets
static CPhidgetInterfaceKitHandle kitHandle;
static CPhidgetServoHandle servoHandle;
/**************************************************
* NAME: static int setup_interface_kit_connection(void)
*
* DESCRIPTION:
* Creates an interface kit object, opens the device for connections and waits for
* attachment.
*
* INPUTS:
* EXTERNALS:
* CPhidgetInterfaceKitHandle kitHandle: An empty handle which will be attached
* to a device.
*
* OUTPUTS:
* EXTERNALS:
* CPhidgetInterfaceKitHandle kitHandle: A handle with a registered phidget.
*
* RETURNS:
* int: 0 if successful, 1 if failure.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
static int setup_interface_kit_connection(void)
{
// create the InterfaceKit object
CPhidgetInterfaceKit_create(&kitHandle);
// open the interfacekit for device connections, -1 opens any serial number available
CPhidget_open((CPhidgetHandle) kitHandle, -1);
// get the program to wait for the interface kit device to be attached
int result;
const char *err;
printf("Waiting for interface kit to be attached...\n");
if ((result = CPhidget_waitForAttachment((CPhidgetHandle) kitHandle, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 1;
}
return 0;
}
/**************************************************
* NAME: static int setup_servo_motor_connection(void)
*
* DESCRIPTION:
* Creates a servo object, opens the device for connections and waits for
* attachment.
*
* INPUTS:
* EXTERNALS:
* CPhidgetServoHandle servoHandle: An empty handle which will be attached
* to a device.
*
* OUTPUTS:
* EXTERNALS:
* CPhidgetServoHandle kitHandle: A handle with a registered phidget.
*
* RETURNS:
* int: 0 if successful, 1 if failure.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
static int setup_servo_motor_connection(void)
{
// create the servo object
CPhidgetServo_create(&servoHandle);
// open the servoHandle for device connections, -1 opens any serial number available
CPhidget_open((CPhidgetHandle) servoHandle, -1);
// get the program to wait for an servoHandle device to be attached
int result;
const char *err;
printf("Waiting for servo to be attached...\n");
if ((result = CPhidget_waitForAttachment((CPhidgetHandle) servoHandle, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 1;
}
return 0;
}
/**************************************************
* NAME: int connect_phidgets(void)
*
* DESCRIPTION:
* Connects to both the interface kit and servo by calling the private
* functions setup_interface_kit_connection() and setup_servo_motor_connection().
*
* INPUTS:
* none
*
* OUTPUTS:
* RETURNS:
* int: 0 if successful, 1 if failure.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
int connect_phidgets(void)
{
// setup a connection to the phidgets we are going to use
if (setup_interface_kit_connection())
return 1;
if (setup_servo_motor_connection())
return 1;
return 0;
}
/**************************************************
* NAME: int get_sensor_value(void)
*
* DESCRIPTION:
* Gets the current sensor value from the interface kit.
*
* INPUTS:
* EXTERNALS:
* CPhidgetInterfaceKitHandle kitHandle: A handle with a registered interface kit.
*
* OUTPUTS:
* RETURN:
* int: The sensor value (0-1000).
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
int get_sensor_value(void)
{
int sensorValue;
CPhidgetInterfaceKit_getSensorValue(kitHandle, SENSOR_ID, &sensorValue);
// return noise reduced value
return responsive_analog_read(sensorValue);
}
/**************************************************
* NAME: void set_servo_position(double position)
*
* DESCRIPTION:
* Sets the position of the servo motor.
*
* INPUTS:
* PARAMETERS:
* double position: The new servo motor position.
* EXTERNALS:
* CPhidgetServoHandle servoHandle: A handle with a registered servo motor.
*
* OUTPUTS:
* none
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
void set_servo_position(double position)
{
CPhidgetServo_setPosition(servoHandle, 0, position);
return;
}
/**************************************************
* NAME: void close_connections(void)
*
* DESCRIPTION:
* Closes the connection to the phidgets.
*
* INPUTS:
* EXTERNALS:
* CPhidgetServoHandle servoHandle: A handle with a registered servo motor.
* CPhidgetInterfaceKitHandle kitHandle: A handle with a registered interface kit.
*
* OUTPUTS:
* none
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
void close_connections(void)
{
CPhidget_close((CPhidgetHandle) kitHandle);
CPhidget_delete((CPhidgetHandle) kitHandle);
CPhidget_close((CPhidgetHandle) servoHandle);
CPhidget_delete((CPhidgetHandle) servoHandle);
return;
}