Commit 229bf8e65b2effdd8d29a86529b61418cc91f3f3

  • avatar
  • mailkivi <git @yous…rce.it.jyu.fi> (Committer)
  • Fri Jan 07 23:28:18 EET 2011
  • avatar
  • mailkivi <git @yous…rce.it.jyu.fi> (Author)
  • Fri Jan 07 23:28:18 EET 2011
Update with package
Makefile
(28 / 0)
  
1# Copyright (C) 2011 Markus Kivioja <mailkivi@jyu.fi>
2# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
3# This is free software: you are free to change and redistribute it.
4# There is NO WARRANTY, to the extent permitted by law.
5
6# The makefile for building, testing and cleaning the distancesensor.
7
8COMPILER = gcc
9OBJECTS = distanceSensor.o sensorFunctions.o
10LIBS = -lphidget21 -lcurses
11EXECUTABLE = distancesensor
12
13test : distancesensor
14 sudo ./distancesensor
15
16$(EXECUTABLE) : $(OBJECTS)
17 $(COMPILER) $(OBJECTS) $(LIBS) -o $(EXECUTABLE)
18
19distanceSensor.o : distanceSensor.c
20 $(COMPILER) -c distanceSensor.c
21
22sensorFunctions.o : sensorFunctions.c
23 $(COMPILER) -c sensorFunctions.c
24
25$(OBJECTS) : sensorFunctions.h
26
27clean :
28 rm *.o $(EXECUTABLE)
distanceSensor.c
(25 / 201)
  
1#include <stdio.h>
2#include <ncurses.h>
3#include <time.h>
4#include "phidget21.h"
5
6#define ERROR_LOG_FILE "sensors_error.log"
7#define SENSOR_0_1_MAX 400
8#define SENSOR_2_3_MIN 80
9#define SENSOR_2_MAX 530
10#define SENSOR_3_MAX 9462/30+16.92 /* Calculated so that the ranges of sensors 2 and 3 doesn't overlap */
11#define UI_X columns/2-12
12
13int rows = 0;
14int columns = 0;
15bool measuring = true;
16
17int printInformation(CPhidgetInterfaceKitHandle interfaceKitHandle)
18{
19 const char* deviceType = "";
20 int serialNumber = 0, version = 0;
21
22 CPhidget_getDeviceType((CPhidgetHandle)interfaceKitHandle, &deviceType);
23 CPhidget_getSerialNumber((CPhidgetHandle)interfaceKitHandle, &serialNumber);
24 CPhidget_getDeviceVersion((CPhidgetHandle)interfaceKitHandle, &version);
25
26 int row = 4;
27 mvprintw(row++, UI_X, "%s", deviceType);
28 mvprintw(row++, UI_X, "Serial Number: %d", serialNumber);
29 mvprintw(row++, UI_X, "Version: %d", version);
30 refresh();
31
32 return 0;
33}
34
35void printMenu()
36{
37 int row = rows/2-3;
38 mvprintw(row++, UI_X, "1. Start measuring");
39 mvprintw(row++, UI_X, "2. Stop measuring");
40 mvprintw(row++, UI_X, "q. Quit");
41 refresh();
42}
43
44void printDistance(float distance, int index)
45{
46 int row = rows/2 + 6;
47 int col = columns/2 - 26;
48
49 if (measuring) {
50 /* Sensors 0 and 1 can't tell the absolute distance. They
51 * can only tell if there's an object closer than some distance.*/
52 if (index < 2)
53 mvprintw(row, col, "There's an object closer than %3.2f cm of the sensor %d.\n", distance, index);
54 else
55 mvprintw(row, col, "The distance is %5.2f cm (measured by the sensor %d).\n", distance, index);
56 refresh();
57 }
58}
59
60int setChangeTrigger(CPhidgetInterfaceKitHandle interfaceKitHandle, int trigger)
61{
62 int sensorCount = 0;
63 CPhidgetInterfaceKit_getSensorCount(interfaceKitHandle, &sensorCount);
64 int i;
65 for (i = 0; i < sensorCount; i++)
66 if (CPhidgetInterfaceKit_setSensorChangeTrigger(interfaceKitHandle, i, trigger))
67 return 1;
68
69 return 0;
70}
71
72int startMeasuring(CPhidgetInterfaceKitHandle interfaceKitHandle)
73{
74 if (measuring) return 1;
75 int succeed = setChangeTrigger(interfaceKitHandle, 1);
76 if (!succeed) {
77 mvprintw(rows/2+6, UI_X, "Measuring...\n");
78 refresh();
79 measuring = true;
80 }
81 return setChangeTrigger(interfaceKitHandle, 1);
82}
83
84int stopMeasuring(CPhidgetInterfaceKitHandle interfaceKitHandle)
85{
86 int succeed = 1;
87 if (measuring)
88 succeed = setChangeTrigger(interfaceKitHandle, 999);
89 move(rows/2+6, 0);
90 clrtoeol();
91 mvprintw(rows/2+6, UI_X, "Measuring is stopped.");
92 refresh();
93 measuring = false;
94 return succeed;
95}
96
971/*
98 * If an object is in the range of the sensor, this function
99 * calculates the distance between the object and the sensor.
100 * @parameter *distPtr: A Pointer to the calculated distance
101 * @parameter index: The index of the sensor
102 * @parameter value: The value of the sensor
103 * @return 1 if the object is in the range of the sensor, 0 otherwise
2 * Copyright (C) 2011 Markus Kivioja <mailkivi@jyu.fi>
3 * License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
4 * This is free software: you are free to change and redistribute it.
5 * There is NO WARRANTY, to the extent permitted by law.
6 *
7 * With this program Phidget Interface Kit and Phidget analog distance sensors
8 * can be used to measure distances between 0.4cm and 150cm.
1049 */
105int calculateDistance(float* distPtr, int index, int value)
106{
107 int inRange = 0;
108 switch(index) {
109 case 0:
110 if (value <= SENSOR_0_1_MAX) {
111 *distPtr = 0.7;
112 inRange = 1;
113 }
114 break;
115 case 1:
116 if (value <= SENSOR_0_1_MAX) {
117 *distPtr = 4;
118 inRange = 1;
119 }
120 break;
121 case 2:
122 if (SENSOR_2_3_MIN <= value && value <= SENSOR_2_MAX) {
123 *distPtr = 2076/((float)value-11);
124 inRange = 1;
125 }
126 break;
127 case 3:
128 if (SENSOR_2_3_MIN <= value && value <= SENSOR_3_MAX) {
129 *distPtr = 9462/((float)value-16.92);
130 inRange = 1;
131 }
132 break;
133 default: break;
134 }
135 return inRange;
136}
13710
138/*
139 * The callback function which is called when the value of a
140 * sensor is changed more than the SensorChangeTrigger
141 */
142int sensorChangeHandler(CPhidgetInterfaceKitHandle interfaceKitHandle, void *userPtr, int index, int value)
143{
144 float distance = 0;
145
146 /* If there's no objects in the range of the sensors, don't print anything. */
147 if (calculateDistance(&distance, index, value))
148 printDistance(distance, index);
149
150 return 0;
151}
11#include <stdio.h>
12#include "sensorFunctions.h"
15213
153int attachHandler(CPhidgetHandle phidgetHandle, void *userPtr)
154{
155 clear();
156 printInformation((CPhidgetInterfaceKitHandle)phidgetHandle);
157 printMenu();
158 stopMeasuring((CPhidgetInterfaceKitHandle)phidgetHandle);
159}
14#define ERROR_LOG_FILE "sensors_error.log"
16015
161int detachHandler(CPhidgetHandle phidgetHandle, void *userPtr)
162{
163 clear();
164 mvprintw(rows/2, columns/2-35, "The device it not attached. Please attach the device or press q to quit.");
165 refresh();
166}
167
168/*
169 * Error logging into a file
170 */
171int errorHandler(CPhidgetHandle phidgetHandle, void *userPtr, int errorCode, const char *errorString)
172{
173 time_t rawtime;
174 time(&rawtime);
175 struct tm * timeInfo;
176 timeInfo = localtime(&rawtime);
177 fprintf((FILE*)userPtr, "%s\tError %d: %s\n", asctime(timeInfo), errorCode, errorString);
178}
179
18016int main(int argc, char* argv[])
181{
182 /* UI related */
183 initscr();
184 raw();
185 noecho();
186 getmaxyx(stdscr, rows, columns);
187
188 /* Create a handle to the device */
17{
18 initUI();
19
20 FILE *errorFile;
21 if (!(errorFile = fopen(ERROR_LOG_FILE, "a+")))
22 errorFile = fopen(ERROR_LOG_FILE, "w+");
23
18924 CPhidgetInterfaceKitHandle interfaceKitHandle = 0;
19025 CPhidgetInterfaceKit_create(&interfaceKitHandle);
191
192 /* Attach callback functions to corresponding events */
193 CPhidgetInterfaceKit_set_OnSensorChange_Handler(interfaceKitHandle, sensorChangeHandler, NULL);
194 CPhidget_set_OnAttach_Handler((CPhidgetHandle)interfaceKitHandle, attachHandler, NULL);
195 CPhidget_set_OnDetach_Handler((CPhidgetHandle)interfaceKitHandle, detachHandler, NULL);
196 FILE *f;
197 if (!(f = fopen(ERROR_LOG_FILE, "a+")))
198 f = fopen(ERROR_LOG_FILE, "w+");
199 CPhidget_set_OnError_Handler((CPhidgetHandle)interfaceKitHandle, errorHandler, f);
200
201 /* Open and attach the device */
202 CPhidget_open((CPhidgetHandle)interfaceKitHandle, -1);
203 CPhidget_waitForAttachment((CPhidgetHandle)interfaceKitHandle, 1000);
26 initInterfaceKitHandle(interfaceKitHandle, errorFile);
20427
205 int status = CPhidget_getDeviceStatus((CPhidgetHandle)interfaceKitHandle, &status);
28 int attached;
29 CPhidget_getDeviceStatus((CPhidgetHandle)interfaceKitHandle, &attached);
20630
207 if (!status)
31 if (!attached)
20832 detachHandler(NULL, NULL);
20933
21034 char choise;
3636 /* The mainloop */
3737 while (1) {
3838 choise = getch();
39 CPhidget_getDeviceStatus((CPhidgetHandle)interfaceKitHandle, &status);
40 if (status) {
39 CPhidget_getDeviceStatus((CPhidgetHandle)interfaceKitHandle, &attached);
40 if (attached) {
4141 switch (choise) {
4242 case 'q':
4343 goto END;
4646 break;
4747 case '2':
4848 stopMeasuring(interfaceKitHandle);
49 break;
49 break;
5050 default: break;
5151 }
5252 }
5858 END:
5959 CPhidget_close((CPhidgetHandle)interfaceKitHandle);
6060 CPhidget_delete((CPhidgetHandle)interfaceKitHandle);
61 fclose(f);
61 fclose(errorFile);
6262 endwin();
6363
6464 return 0;
sensorFunctions.c
(214 / 0)
  
1/*
2 * Copyright (C) 2011 Markus Kivioja <mailkivi@jyu.fi>
3 * License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
4 * This is free software: you are free to change and redistribute it.
5 * There is NO WARRANTY, to the extent permitted by law.
6 *
7 * Functions for managing Phidget Interface Kit and Phidget analog distance sensors.
8 */
9
10#include <stdio.h>
11#include <ncurses.h>
12#include <time.h>
13#include "sensorFunctions.h"
14
15#define SENSOR_0_1_MAX 400
16#define SENSOR_2_3_MIN 80
17#define SENSOR_2_MAX 530
18#define SENSOR_3_MAX 490
19#define UI_X columns/2-12
20
21int rows = 0;
22int columns = 0;
23bool measuring = true;
24
25/*
26 * Initializes the screen for the UI
27 */
28void initUI()
29{
30 initscr();
31 curs_set(0);
32 raw();
33 noecho();
34 getmaxyx(stdscr, rows, columns);
35}
36
37/*
38 * Initializes the handle for the interface kit
39 */
40void initInterfaceKitHandle(CPhidgetInterfaceKitHandle interfaceKitHandle, FILE *errorFile)
41{
42 /* Attach callback functions to corresponding events */
43 CPhidgetInterfaceKit_set_OnSensorChange_Handler(interfaceKitHandle, sensorChangeHandler, NULL);
44 CPhidget_set_OnAttach_Handler((CPhidgetHandle)interfaceKitHandle, attachHandler, NULL);
45 CPhidget_set_OnDetach_Handler((CPhidgetHandle)interfaceKitHandle, detachHandler, NULL);
46 CPhidget_set_OnError_Handler((CPhidgetHandle)interfaceKitHandle, errorHandler, errorFile);
47
48 /* Open and attach the device */
49 CPhidget_open((CPhidgetHandle)interfaceKitHandle, -1);
50 CPhidget_waitForAttachment((CPhidgetHandle)interfaceKitHandle, 1000);
51}
52
53void printInformation(CPhidgetInterfaceKitHandle interfaceKitHandle)
54{
55 const char* deviceType = "";
56 int serialNumber = 0, version = 0;
57
58 CPhidget_getDeviceType((CPhidgetHandle)interfaceKitHandle, &deviceType);
59 CPhidget_getSerialNumber((CPhidgetHandle)interfaceKitHandle, &serialNumber);
60 CPhidget_getDeviceVersion((CPhidgetHandle)interfaceKitHandle, &version);
61
62 int row = 4;
63 mvprintw(row++, UI_X, "%s", deviceType);
64 mvprintw(row++, UI_X, "Serial Number: %d", serialNumber);
65 mvprintw(row++, UI_X, "Version: %d", version);
66 refresh();
67}
68
69void printMenu()
70{
71 int row = rows/2-3;
72 mvprintw(row++, UI_X, "1. Start measuring");
73 mvprintw(row++, UI_X, "2. Stop measuring");
74 mvprintw(row++, UI_X, "q. Quit");
75 refresh();
76}
77
78void printDistance(float distance, int index)
79{
80 int row = rows/2 + 6;
81
82 if (measuring) {
83 mvprintw(row + index, UI_X - 12, "Sensor %d: Last measured distance was %5.2f cm.\n", index, distance);
84 refresh();
85 }
86}
87
88/*
89 * Sets the minimum value difference that triggers the sensorChangeHandler for all the sensors.
90 */
91int setChangeTrigger(CPhidgetInterfaceKitHandle interfaceKitHandle, int trigger)
92{
93 int sensorCount = 0;
94 CPhidgetInterfaceKit_getSensorCount(interfaceKitHandle, &sensorCount);
95 int i;
96 for (i = 0; i < sensorCount; i++)
97 if (CPhidgetInterfaceKit_setSensorChangeTrigger(interfaceKitHandle, i, trigger))
98 return 1;
99
100 return 0;
101}
102
103int startMeasuring(CPhidgetInterfaceKitHandle interfaceKitHandle)
104{
105 if (measuring) return 1;
106 int succeed = setChangeTrigger(interfaceKitHandle, 1);
107 if (!succeed) {
108 mvprintw(rows/2+4, UI_X, "Measuring...\n");
109 refresh();
110 measuring = true;
111 }
112 return setChangeTrigger(interfaceKitHandle, 1);
113}
114
115int stopMeasuring(CPhidgetInterfaceKitHandle interfaceKitHandle)
116{
117 int succeed = 1;
118 if (measuring) {
119 succeed = setChangeTrigger(interfaceKitHandle, 999);
120 measuring = false;
121 }
122 mvprintw(rows/2+4, UI_X, "Measuring is stopped.");
123 refresh();
124 return succeed;
125}
126
127/*
128 * If an object is in the range of the sensor, this function
129 * calculates the distance between the object and the sensor.
130 * @parameter *distPtr: A Pointer to the calculated distance
131 * @parameter index: The index of the sensor
132 * @parameter value: The value of the sensor
133 * @return 1 if the object is in the range of the sensor, 0 otherwise
134 */
135int calculateDistance(float* distPtr, int index, int value)
136{
137 int inRange = 0;
138 switch(index) {
139 case 0:
140 if (value <= SENSOR_0_1_MAX) {
141 *distPtr = 0.7;
142 inRange = 1;
143 }
144 break;
145 case 1:
146 if (value <= SENSOR_0_1_MAX) {
147 *distPtr = 4;
148 inRange = 1;
149 }
150 break;
151 case 2:
152 if (SENSOR_2_3_MIN <= value && value <= SENSOR_2_MAX) {
153 *distPtr = 2076/((float)value - 11);
154 inRange = 1;
155 }
156 break;
157 case 3:
158 if (SENSOR_2_3_MIN <= value && value <= SENSOR_3_MAX) {
159 *distPtr = 9462/((float)value - 16.92);
160 inRange = 1;
161 }
162 break;
163 default: break;
164 }
165 return inRange;
166}
167
168/*
169 * The callback function which is called when the value of a
170 * sensor is changed more than the SensorChangeTrigger
171 */
172int sensorChangeHandler(CPhidgetInterfaceKitHandle interfaceKitHandle, void *userPtr, int index, int value)
173{
174 float distance = 0;
175
176 /* If there's no objects in the range of the sensors, don't print anything. */
177 if (calculateDistance(&distance, index, value))
178 printDistance(distance, index);
179
180 return 0;
181}
182
183/*
184 * The callback function which is called when the device is attached
185 */
186int attachHandler(CPhidgetHandle phidgetHandle, void *userPtr)
187{
188 clear();
189 printInformation((CPhidgetInterfaceKitHandle)phidgetHandle);
190 printMenu();
191 stopMeasuring((CPhidgetInterfaceKitHandle)phidgetHandle);
192}
193
194/*
195 * The callback function which is called when the device is detached
196 */
197int detachHandler(CPhidgetHandle phidgetHandle, void *userPtr)
198{
199 clear();
200 mvprintw(rows/2, columns/2-35, "The device it not attached. Please attach the device or press q to quit.");
201 refresh();
202}
203
204/*
205 * Error logging into a file. userPtr is used as a pointer to the error file.
206 */
207int errorHandler(CPhidgetHandle phidgetHandle, void *userPtr, int errorCode, const char *errorString)
208{
209 time_t rawtime;
210 time(&rawtime);
211 struct tm * timeInfo;
212 timeInfo = localtime(&rawtime);
213 fprintf((FILE*)userPtr, "%s\tError %d: %s\n", asctime(timeInfo), errorCode, errorString);
214}
sensorFunctions.h
(32 / 0)
  
1/*
2 * Copyright (C) 2011 Markus Kivioja <mailkivi@jyu.fi>
3 * License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
4 * This is free software: you are free to change and redistribute it.
5 * There is NO WARRANTY, to the extent permitted by law.
6 *
7 * Function prototypes
8 */
9
10#ifndef SENSOR_FUNCTIONS
11#define SENSOR_FUNCTIONS
12
13#include "phidget21.h"
14
15void initUI();
16void initInterfaceKitHandle(CPhidgetInterfaceKitHandle, FILE*);
17
18void printInformation(CPhidgetInterfaceKitHandle);
19void printMenu();
20void printDistance(float, int);
21
22int setChangeTrigger(CPhidgetInterfaceKitHandle, int);
23int startMeasuring(CPhidgetInterfaceKitHandle);
24int stopMeasuring(CPhidgetInterfaceKitHandle);
25int calculateDistance(float*, int, int);
26
27int sensorChangeHandler(CPhidgetInterfaceKitHandle, void*, int, int);
28int attachHandler(CPhidgetHandle, void*);
29int detachHandler(CPhidgetHandle, void*);
30int errorHandler(CPhidgetHandle, void*, int, const char*);
31
32#endif //SENSOR_FUNCTIONS