-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminator.cpp
2056 lines (1679 loc) · 61.4 KB
/
terminator.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Autonomous robot, Elimia
* Author. Daniel Díaz Bejarano
*/
#include <stdio.h>
#include <libpowerbutton.h>
#include <phidget21.h>
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <sys/time.h>
#include <vector>
#include <signal.h>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <sstream>
#include <string>
using namespace cv;
using namespace std;
//GLOBAL VARIABLES
//alarm for obstacles
volatile sig_atomic_t keep_going = 0;
//alarm for frames: coordination between capture_frame AND calculate_matches
volatile sig_atomic_t frame_alert = 0;
bool obstacle =false;
bool moving=false;
bool look_for_box=true; //Used to synchronize the box_recognition and the image_recognition
bool look_for_image=false; //We set the box_recognition to work first and the image_recognition to wait for a box to recognize.
bool look_for_base=false;
bool box_true_base_false = true;
//global value: 8
int source=8;
int sensor0_value=0;
int sensor1_value=0;
//touch sensor boolean
bool extreme = false;
CPhidgetMotorControlHandle motoControl = 0;
char direction= 'N';
Mat *templa = new Mat[11];
Mat descriptorsTempla; //Descriptors for template
Mat descriptorsCamera; //Descriptors for camera
//Mat outCamera;
Mat outTempla; //For the template
Mat chosen_pic_box, chosen_pic_base;
CvMat* image = 0, *gray=0; //For the camera
//CAPTURE
CvCapture* capture;
std::vector<KeyPoint> keypointsCamera;
//CENTRAL DISTANCE VARIABLES
float central_x = 0;
float central_y = 0;
bool template_found=false;
//SERVO CONTROLLER
CPhidgetAdvancedServoHandle servo;
vector <KeyPoint> final_items;
int chosen_box,chosen_base;
int AttachHandler(CPhidgetHandle MC, void *userptr)
{
int serialNo;
const char *name;
CPhidget_getDeviceName (MC, &name);
CPhidget_getSerialNumber(MC, &serialNo);
printf("%s %10d attached!\n", name, serialNo);
return 0;
}
int DetachHandler(CPhidgetHandle MC, void *userptr)
{
int serialNo;
const char *name;
CPhidget_getDeviceName (MC, &name);
CPhidget_getSerialNumber(MC, &serialNo);
printf("%s %10d detached!\n", name, serialNo);
return 0;
}
int ErrorHandler(CPhidgetHandle MC, void *userptr, int ErrorCode, const char *Description)
{
printf("Error handled. %d - %s\n", ErrorCode, Description);
return 0;
}
int InputChangeHandlerMo(CPhidgetMotorControlHandle MC, void *usrptr, int Index, int State)
{
//printf("Input %d > State: %d\n", Index, State);
return 0;
}
int VelocityChangeHandler(CPhidgetMotorControlHandle MC, void *usrptr, int Index, double Value)
{
//printf("Motor %d > Current Speed: %f\n", Index, Value);
return 0;
}
int CurrentChangeHandler(CPhidgetMotorControlHandle MC, void *usrptr, int Index, double Value)
{
//printf("Motor: %d > Current Draw: %f\n", Index, Value);
return 0;
}
int display_MO_properties(CPhidgetMotorControlHandle phid)
{
int serialNo, version, numInputs, numMotors;
const char* ptr;
CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
CPhidgetMotorControl_getInputCount(phid, &numInputs);
CPhidgetMotorControl_getMotorCount(phid, &numMotors);
printf("%s\n", ptr);
printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
printf("# Inputs: %d\n# Motors: %d\n", numInputs, numMotors);
return 0;
}
//SERVO CONTROLLING
int PositionChangeHandler(CPhidgetAdvancedServoHandle ADVSERVO, void *usrptr, int Index, double Value)
{
printf("Motor: %d > Current Position: %f\n", Index, Value);
return 0;
}
int display_SERVO_properties(CPhidgetAdvancedServoHandle phid)
{
int serialNo, version, numMotors;
const char* ptr;
CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
CPhidgetAdvancedServo_getMotorCount (phid, &numMotors);
printf("%s\n", ptr);
printf("Serial Number: %10d\nVersion: %8d\n# Motors: %d\n", serialNo, version, numMotors);
return 0;
}
/*************************InterfaceKit functions***************************///(sensor)
//callback that will run if an output changes.
//Index - Index of the output that generated the event, State - boolean (0 or 1) representing the output state (on or off)
int OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
{
printf("Digital Output: %d > State: %d\n", Index, State);
return 0;
}
int go_backwards(){
direction='B';
CPhidgetMotorControl_setAcceleration (motoControl, 0, 100.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 100.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, 100.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 100.00);
}
int go_forward(){
direction='F';
CPhidgetMotorControl_setAcceleration (motoControl, 0, -80.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, -40.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, -80.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, -40.00);
}
int turn_right(){
direction='R';
//Stop the motor by decreasing speed to 0;
CPhidgetMotorControl_setVelocity (motoControl, 0, 100.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 100.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, -100.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, -100.00);
}
int turn_left(){
direction='L';
//Stop the motor by decreasing speed to 0;
CPhidgetMotorControl_setVelocity (motoControl, 0, -100.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, -100.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 100.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, 100.00);
}
void go_forward_slowly(){
direction='F';
CPhidgetMotorControl_setAcceleration (motoControl, 0, -40.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, -20.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, -40.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, -20.00);
}
void turn_left_slowly(){
direction='L';
CPhidgetMotorControl_setAcceleration (motoControl, 0, -50.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, -25.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, 50.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 25.00);
}
void turn_right_slowly(){
direction='R';
CPhidgetMotorControl_setAcceleration (motoControl, 1, -50.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, -25.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 50.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 25.00);
}
void go_backwards_slowly(){
direction='B';
CPhidgetMotorControl_setAcceleration (motoControl, 0, 80.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 30.00);
CPhidgetMotorControl_setAcceleration (motoControl, 1, 80.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 30.00);
}
void stop(){
direction='S';
//STOP
CPhidgetMotorControl_setAcceleration (motoControl, 1, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 0.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 0.00);
}
int wait(unsigned long milisec)
{
struct timespec req={0};
time_t sec=(int)(milisec/1000);
milisec=milisec-(sec*1000);
req.tv_sec=sec;
req.tv_nsec=milisec*1000000L;
while(nanosleep(&req,&req)==-1)
continue;
return 1;
}
/* The signal handler just clears the flag and re-enables itself. */
void catch_alarm (int sig){
printf("Alarm run out!\n");
keep_going = 0;
signal (sig, catch_alarm);
}
void set_alarm(int time){
//Set an alarm to go off in a little while.
printf("Alarm set!\n");
// Establish a handler for SIGALRM signals.
keep_going=1;
signal(SIGALRM, catch_alarm);
alarm(time);
//Check the flag once in a while to see when to quit.
//while (keep_going)
//printf("Timer Waiting\n");
}
void close_grabber(){
//Step 1: Position 40.00 - also engage servo
printf("Move to position 100.00 and engage. Press any key to Continue\n");
CPhidgetAdvancedServo_setPosition (servo, 0, 120.00);
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
//Step 4: Position 150.00
printf("Move to position 150.00. Press any key to Continue\n");
}
//ALARM METHODS FOR OBJECT GRABBING
//frame_alert
void catch_alarm2 (int sig){
printf("Frame alarm run out!\n");
frame_alert = 0;
signal (sig, catch_alarm2);
}
void set_alarm2 (int time){
//Set an alarm to go off in a little while.
printf("Frame alarm!\n");
// Establish a handler for SIGALRM signals.
frame_alert=1;
signal(SIGALRM, catch_alarm2);
alarm(time);
}
void turn_around(){
//check sensor values to decide which turn is better
if(sensor0_value>sensor1_value)
if (direction != 'L')
turn_left_slowly();
if(sensor0_value<=sensor1_value)
if (direction != 'R')
turn_right_slowly();
wait(2000);
if (direction != 'F')
go_forward();
extreme=false;
}
void obstacle_found(){
moving=true;
if (direction != 'B')
go_backwards();
set_alarm(1);
//while alarm is on, check if EXTREME value is on(if touch is activated)
while (keep_going && !extreme);
if(extreme){
printf("Sonar interrupted!!");
obstacle=false;
return;
}
//action after alarm
//check sensor values to decide which turn is better
if(sensor0_value>sensor1_value)
if (direction != 'L')
turn_left();
if(sensor0_value<=sensor1_value)
if (direction != 'R')
turn_right();
set_alarm(1);
//while alarm is on, check if EXTREME value is on(if touch is activated)
while (keep_going && !extreme);
if(extreme){
printf("Sonar interrupted!!");
obstacle=false;
return;
}
if (direction != 'F')
go_forward();
obstacle=false;
printf("Sonar finished!");
moving=false;
}
//callback that will run if the sensor value changes by more than the OnSensorChange trigger.
//Index - Index of the sensor that generated the event, Value - the sensor read value
//DIGITAL INPUTS
int InputChangeHandlerKi(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
{
if (!extreme){
switch(Index)
{ //Front sensor
case 0:
case 1:
//we have touch
if(State == 1)
{
//set extreme boolean to true
extreme=true;
printf("OBSTACLE FOUND WITH TOUCH SENSOR\n\n");
//go back
if (direction != 'B')
go_backwards();
set_alarm(2);
//do nothing while alarm is on
while (keep_going);
//turn around
printf("im TURNING\n\n\n\n\n\n\n");
turn_around();
}
break;
}
//printf("Digital Input: %d > State: %d\n", Index, State);
}
return 0;
}
//ANALOG INPUTS
int SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int Value)
{
moving=true;
//WE FOUND TEMPLATE=> IGNORE SENSORS
if (!template_found){
if(!obstacle){ //
//printf("\n\n\nSENSORS WORKING\n\n\n\n");
//printf("Sensor: %d > Value: %d\n", Index, Value);
//int sensorValue=0;
//CPhidgetInterfaceKit_getSensorValue(ifKit,0,&sensorValue);
switch(Index)
{
case 0:
printf("\n");
//printf("RIGHT IR WORKING\n\n");
if (source == 0 || source == 8 ){
if(Value < 300)
{
if (direction != 'F')
go_forward();
source=8;
}
else
{
if(Value >= 300 && Value < 400){
//printf("Sensor: %d between 300 and 400\n", Index);
source = 0;
if (direction != 'L')
turn_left();
}
else
{
//printf("Sensor: %d over 400\n", Index);
if (direction != 'B')
go_backwards();
}
}
}
sensor0_value=Value;
break;
case 1:
printf("\n");
//printf("LEFT IR WORKING\n\n");
if (source == 1 || source == 8 ){
if(Value < 300)
{
if (direction != 'F')
go_forward();
source=8;
}
else
{
if(Value >= 300 && Value < 400){
//printf("Sensor: %d between 300 and 400\n", Index);
source = 1;
if (direction != 'R')
turn_right();
}
else
{
//printf("Sensor: %d over 400\n", Index);
if (direction != 'B')
go_backwards();
}
}
}
sensor1_value=Value;
break;
case 2:
if (source == 8 ){
if(Value <= 18){
printf("\n");
// printf("SONAR WORKING\n\n");
//printf("Sensor: %d > Value: %d\n", Index, Value);
obstacle=true;
obstacle_found();
}
}
break;
}
}//obstacle
moving=false;
}//template_found
return 0;
}
int display_IK_properties(CPhidgetInterfaceKitHandle phid)
{
int serialNo, version, numInputs, numOutputs, numSensors, triggerVal, ratiometric, i;
const char* ptr;
CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
CPhidgetInterfaceKit_getInputCount(phid, &numInputs);
CPhidgetInterfaceKit_getOutputCount(phid, &numOutputs);
CPhidgetInterfaceKit_getSensorCount(phid, &numSensors);
CPhidgetInterfaceKit_getRatiometric(phid, &ratiometric);
printf("%s\n", ptr);
printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
printf("# Digital Inputs: %d\n# Digital Outputs: %d\n", numInputs, numOutputs);
printf("# Sensors: %d\n", numSensors);
printf("Ratiometric: %d\n", ratiometric);
for(i = 0; i < numSensors; i++)
{
CPhidgetInterfaceKit_getSensorChangeTrigger (phid, i, &triggerVal);
printf("Sensor#: %d > Sensitivity Trigger: %d\n", i, triggerVal);
}
return 0;
}
/*************************InterfaceKit functions end***************************/ //(Sensor)
int button_pressed_method(){
printf("Button pressed %i times.\n",power_button_get_value());
while(power_button_get_value()<1)
{
sleep(1);
//printf("Button pressed %i times.\n",power_button_get_value());
}
power_button_reset();
return 0;
}
void open_grabber(){
CPhidgetAdvancedServo_setPosition (servo, 0, 210.00);
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
set_alarm(1);
while (keep_going);
//Step 7: Disengage
CPhidgetAdvancedServo_setEngaged(servo, 0, 0);
}
//CHANGE PRIORITY OF TOUCH OVER IR!!!!!!!!
//CHANGE PRIORITY OF TOUCH OVER IR!!!!!!!!
//CHANGE PRIORITY OF TOUCH OVER IR!!!!!!!!
void *motorIK_method(void *threadid)
{
int result, i;
const char *err;
int numSensors;
//Declare a motor control handle
motoControl = 0;
//create the motor control object
CPhidgetMotorControl_create(&motoControl);
//Declare an InterfaceKit handle
CPhidgetInterfaceKitHandle ifKit = 0;
//create the InterfaceKit object
CPhidgetInterfaceKit_create(&ifKit);
//****************************************SERVO CONTROL***********************************//
//int result;
double curr_pos;
//const char *err;
double minAccel, maxVel;
//Declare an advanced servo handle
servo = 0;
//create the advanced servo object
CPhidgetAdvancedServo_create(&servo);
//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
CPhidget_set_OnAttach_Handler((CPhidgetHandle)servo, AttachHandler, NULL);
CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, DetachHandler, NULL);
CPhidget_set_OnError_Handler((CPhidgetHandle)servo, ErrorHandler, NULL);
//Registers a callback that will run when the motor position is changed.
//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetAdvancedServo_set_OnPositionChange_Handler(servo, PositionChangeHandler, NULL);
//open the device for connections
CPhidget_open((CPhidgetHandle)servo, -1);
//get the program to wait for an advanced servo device to be attached
printf("Waiting for Phidget to be attached....");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)servo, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
//Display the properties of the attached device
display_SERVO_properties(servo);
//****************************************SERVO CONTROL***********************************//
//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
//MotorControl
CPhidget_set_OnAttach_Handler((CPhidgetHandle)motoControl, AttachHandler, NULL);
CPhidget_set_OnDetach_Handler((CPhidgetHandle)motoControl, DetachHandler, NULL);
CPhidget_set_OnError_Handler((CPhidgetHandle)motoControl, ErrorHandler, NULL);
//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifKit, AttachHandler, NULL);
CPhidget_set_OnDetach_Handler((CPhidgetHandle)ifKit, DetachHandler, NULL);
CPhidget_set_OnError_Handler((CPhidgetHandle)ifKit, ErrorHandler, NULL);
//Registers a callback that will run if an input changes.
//Requires the handle for the Phidget, the function that will be called, and a arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetMotorControl_set_OnInputChange_Handler (motoControl, InputChangeHandlerMo, NULL);
//Registers a callback that will run if a motor changes.
//Requires the handle for the Phidget, the function that will be called, and a arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetMotorControl_set_OnVelocityChange_Handler (motoControl, VelocityChangeHandler, NULL);
//Registers a callback that will run if the current draw changes.
//Requires the handle for the Phidget, the function that will be called, and a arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetMotorControl_set_OnCurrentChange_Handler (motoControl, CurrentChangeHandler, NULL);
//Registers a callback that will run if an input changes.
//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetInterfaceKit_set_OnInputChange_Handler (ifKit, InputChangeHandlerKi, NULL);
//Registers a callback that will run if the sensor value changes by more than the OnSensorChange trig-ger.
//Requires the handle for the IntefaceKit, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetInterfaceKit_set_OnSensorChange_Handler (ifKit, SensorChangeHandler, NULL);
//Registers a callback that will run if an output changes.
//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetInterfaceKit_set_OnOutputChange_Handler (ifKit, OutputChangeHandler, NULL);
//open the motor control for device connections
CPhidget_open((CPhidgetHandle)motoControl, -1);
//get the program to wait for a motor control device to be attached
printf("Waiting for MotorControl to be attached....");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)motoControl, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
//open the interfacekit for device connections
CPhidget_open((CPhidgetHandle)ifKit, -1);
//get the program to wait for an interface kit device to be attached
printf("Waiting for interface kit to be attached....");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)ifKit, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
//Display the properties of the attached motor control device
display_MO_properties(motoControl);
display_IK_properties(ifKit);
//read motor control event data
printf("Reading.....\n");
//*******************************************************************//
power_button_reset();
printf("Button pressed %i times.\n",power_button_get_value());
while(power_button_get_value()<1)
{
sleep(1);
//printf("Button pressed %i times.\n",power_button_get_value());
}
power_button_reset();
go_forward();
printf("Button pressed %i times.\n",power_button_get_value());
while(power_button_get_value()<1)
{
sleep(1);
//printf("Button pressed %i times.\n",power_button_get_value());
}
power_button_reset();
//Stop the motor by decreasing speed to 0;
CPhidgetMotorControl_setAcceleration (motoControl, 1, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 0.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 0.00);
//printf("SPEED OF 0 SET to 0\n\n");
//Step 4: Close Session
while (moving == true){
CPhidgetMotorControl_setAcceleration (motoControl, 1, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 0.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 0.00);
}
open_grabber();
CPhidget_close((CPhidgetHandle)motoControl);
CPhidget_delete((CPhidgetHandle)motoControl);
printf("Sensor closing");
CPhidget_close((CPhidgetHandle)ifKit);
CPhidget_delete((CPhidgetHandle)ifKit);
printf("Sensor closed");
//CLOSE SERVO
printf("Servo closing");
CPhidget_close((CPhidgetHandle)servo);
CPhidget_delete((CPhidgetHandle)servo);
printf("Servo closed");
//all done, exit
pthread_exit(NULL);
return 0;
}
int inform_templates(){
//Alphabetic ordering in the array of templas
templa[0] = imread("images/celebes.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[0].data || !templa[0].data )
{ std::cout<< " --(!) Error reading image cellebes" << std::endl; return -1; }
templa[1] = imread("images/ferrari.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[1].data || !templa[1].data )
{ std::cout<< " --(!) Error reading image ferrari" << std::endl; return -1; }
templa[2] = imread("images/fry.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[2].data || !templa[2].data )
{ std::cout<< " --(!) Error reading image fry" << std::endl; return -1; }
templa[3] = imread("images/iron.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[3].data || !templa[3].data )
{ std::cout<< " --(!) Error reading image iron" << std::endl; return -1; }
templa[4] = imread("images/mario.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[4].data || !templa[4].data )
{ std::cout<< " --(!) Error reading image mario" << std::endl; return -1; }
templa[5] = imread("images/starry.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[5].data || !templa[5].data )
{ std::cout<< " --(!) Error reading image starry" << std::endl; return -1; }
templa[6] = imread("images/terminator.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[6].data || !templa[6].data )
{ std::cout<< " --(!) Error reading image terminator" << std::endl; return -1; }
templa[7] = imread("images/thor.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[7].data || !templa[7].data )
{ std::cout<< " --(!) Error reading image thor " << std::endl; return -1; }
templa[8] = imread("images/walle.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[8].data || !templa[8].data )
{ std::cout<< " --(!) Error reading image walle" << std::endl; return -1; }
templa[9] = imread("images/base1small.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[9].data || !templa[9].data )
{ std::cout<< " --(!) Error reading image strangebase" << std::endl; return -1; }
templa[10] = imread("images/base2small.png", CV_LOAD_IMAGE_GRAYSCALE );
if( !templa[10].data || !templa[10].data )
{ std::cout<< " --(!) Error reading image queen" << std::endl; return -1; }
return 0;
}
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID, " << tid << endl;
pthread_exit(NULL);
}
void inform_template_descriptors_box(){
std::vector<KeyPoint> keypointsTempla;
//HESSIANS
SurfFeatureDetector surf(450.);
SurfDescriptorExtractor surfDesc;
surf.detect(chosen_pic_box, keypointsTempla);
//drawKeypoints(chosen_pic, keypointsTempla, outTempla, Scalar(255,255,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//imshow("SURF detector Templa", outTempla);
surfDesc.compute(chosen_pic_box, keypointsTempla, descriptorsTempla);
printf("Template: Descriptor cols: %i \n Descriptor rows: %i\n",descriptorsTempla.cols,descriptorsTempla.rows);
//imshow("SURF detector Templa", outTempla);
}
void inform_template_descriptors_base(){
std::vector<KeyPoint> keypointsTempla;
//HESSIANS
SurfFeatureDetector surf(450.);
SurfDescriptorExtractor surfDesc;
surf.detect(chosen_pic_base, keypointsTempla);
//drawKeypoints(chosen_pic, keypointsTempla, outTempla, Scalar(255,255,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//imshow("SURF detector Templa", outTempla);
surfDesc.compute(chosen_pic_base, keypointsTempla, descriptorsTempla);
printf("Template: Descriptor cols: %i \n Descriptor rows: %i\n",descriptorsTempla.cols,descriptorsTempla.rows);
//imshow("SURF detector Templa", outTempla);
}
void grab_object(float x){
//CHECK RANGE OF X
if (x> 400){
turn_right_slowly();
wait(200);
}
else if (x<240){
turn_left_slowly();
wait(200);
}
go_forward();
wait(5000);
close_grabber();
wait(1200);
turn_around();
}
void release_object(float x){
//CHECK RANGE OF X
if (x> 400){
turn_right_slowly();
wait(200);
}
else if (x<240){
turn_left_slowly();
wait(200);
}
go_forward();
wait(4800);
CPhidgetMotorControl_setAcceleration (motoControl, 1, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 1, 0.00);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 0.00);
CPhidgetMotorControl_setVelocity (motoControl, 0, 0.00);
open_grabber();
wait(2000);
go_backwards();
wait(2000);
turn_around();
}
void *calculate_matches(void *threadid){ //FERRARI CODE
//COLUMNS: ATTRIBUTES
//column 0: (float)distance_threshold
//cloumn 1: (int)neighbours_threshold
//column 2: (int)match_threshold
int i,j,k;
CvMat* image = 0, *gray=0; //For the camera
Mat descriptorsCamera; //Descriptors for camera
Mat outCamera;
int key = 0;
int sum;
static CvScalar red_color[] ={0,0,255};
int times_check=0;
int times_verified=0;
//set different hessian for the surfs
//BIGGER VALUES FOR
SurfFeatureDetector surf_frame(750.);//750
SurfDescriptorExtractor surfDesc_frame;
int best_match_threshold;
float first_distance;
//sleep(3);
float box_x=0;
float box_y=0;
float base_x=0;
float base_y=0;
while( key!='q' )//key != 'q'
{
while(!look_for_image); //Wait for the box_detection thread
//DISTINGUISH BETWEEN BOX TEMPLATE AND BASE TEMPLATE
//box_true_base_false
if (box_true_base_false){
inform_template_descriptors_box();
//INFORM THRESHOLDS
//TEMPLATE CASING
switch (chosen_box){
//fry
case 2:
first_distance=0.32;
best_match_threshold = 15;
break;
//iron
case 3:
first_distance=0.32;
best_match_threshold = 22;
break;
//starry
case 5:
first_distance=0.34;
best_match_threshold = 22;
break;
default:
first_distance=0.25;
best_match_threshold = 22;
break;
}
}
else{
//INFORM THRESHOLDS
inform_template_descriptors_base();
first_distance=0.275;
best_match_threshold = 14;
}
printf("Distance criterion: %f, Match threashold: %d\n\n",first_distance,best_match_threshold);
printf("IM IN IMAGE DETECTION THREADDDD\n");
//DIFFICULT FRAMES COULD RESULT IN FAIL
int firstFrame = gray == 0;
//was declared inside
IplImage* frame = cvQueryFrame(capture);
if(!frame){
break;
printf("Frame error\n");
}
if(!gray)
{
//CHANGED SIZE OF IMAGE
image = cvCreateMat(frame->height, frame->width, CV_8UC1);
}
//Convert the RGB image obtained from camera into Grayscale
cvCvtColor(frame, image, CV_BGR2GRAY);
//Define sequence for storing surf keypoints and descriptors
std::vector<KeyPoint> keypointsCamera;
Mat cameraDescriptors;
//Extract SURF points by initializing parameters
//SECOND PARAMETER IS params.extended
// 0 means basic descriptors (64 elements each),
// 1 means extended descriptors (128 elements each)
surf_frame.detect(image, keypointsCamera);
//drawKeypoints(image, keypointsCamera, outCamera, Scalar(255,255,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//namedWindow("SURF detector camera");