blob: 89f168d4ed5d038bec53fb08d9e05e6e1f91be9d [file] [log] [blame]
Brian Silverman6ae77dd2013-03-29 22:28:08 -07001#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <sys/mman.h>
5#include <errno.h>
6#include <string.h>
7#include <vector>
8#include <netinet/in.h>
9#include <netinet/tcp.h>
10#include <arpa/inet.h>
11#include "opencv2/opencv.hpp"
12
13#include "aos/common/time.h"
14#include "aos/atom_code/camera/Buffers.h"
15#include "aos/externals/libjpeg/include/jpeglib.h"
16
17#include "vision/OpenCVWorkTask.h"
18#include "vision/CameraProcessor.h"
19#include "vision/BinaryServer.h"
20#include "vision/PacketNotifier.h"
21#include "vision/JPEGRoutines.h"
22
23
24
25namespace frc971 {
26namespace vision {
27
28} // namespace vision
29} // namespace frc971
30
31void reciever_main(frc971::vision::PacketNotifier *notify){
32 frc971::vision::BinaryServer test(8020,notify);
33}
34
35#include "frc971/queues/CameraTarget.q.h"
36using frc971::vision::targets;
37
38#include <iostream>
39void sender_main(frc971::vision::PacketNotifier *notify){
40 ::aos::InitNRT();
41 ::aos::camera::Buffers buffers;
42 CvSize size;
43 size.width = ::aos::camera::Buffers::Buffers::kWidth;
44 size.height = ::aos::camera::Buffers::Buffers::kHeight;
45
46 // create processing object
47 ProcessorData processor(size.width, size.height, false);
48
49 printf("started sender main\n");
50 while(true){
51 //usleep(7500);
52 size_t data_size;
53 timeval timestamp_timeval;
54 const void *image = buffers.GetNext(
55 true, &data_size, &timestamp_timeval, NULL);
56 ::aos::time::Time timestamp(timestamp_timeval);
57
58 //TODO(pschuh): find proper way to cast away const for this: :(
59 // parker you prolly want const_cast<type>(var);
60 printf("\nNew Image Recieved\n");
61 std::cout << timestamp << std::endl;
62 unsigned char *buffer = (unsigned char *)notify->GetBuffer();
63 frc971::vision::process_jpeg(buffer, (unsigned char *)image, data_size);
64 // build the headers on top of the buffer
65 cvSetData(processor.src_header_image,
66 buffer,
67 ::aos::camera::Buffers::Buffers::kWidth * 3);
68
69 // transform the buffer into targets
70 processor.threshold(buffer);
71 processor.getContours();
72 processor.filterToTargets();
73
74 // could be used for debug ie drawContours
75 //std::vector<std::vector<cv::Point> > target_contours;
76 //std::vector<std::vector<cv::Point> > best_contours;
77 std::vector<Target>::iterator target_it;
78 Target *best_target = NULL;
79 // run through the targets
80 for(target_it = processor.target_list.begin();
81 target_it != processor.target_list.end(); target_it++){
82 //target_contours.push_back(*(target_it->this_contour));
83 // select the highest target
84 if (best_target == NULL) {
85 best_target = &*target_it;
86 } else {
87 if (target_it->height < best_target->height) {
88 best_target = &*target_it;
89 }
90 }
91 }
92 // if we found one then send it on
93 if (best_target != NULL) {
94 targets.MakeWithBuilder()
95 .percent_azimuth_off_center(
96 best_target->rect.centroid.y / (double)::aos::camera::Buffers::kHeight - 0.5)
97 .percent_elevation_off_center(
98 best_target->rect.centroid.x / (double)::aos::camera::Buffers::kWidth - 0.5)
99 .timestamp(timestamp.ToNSec())
100 .Send();
101 }
102 notify->Notify();
103 }
104 ::aos::Cleanup();
105}
106
107
108int main(int /*argc*/, char** /*argv*/){
109 frc971::vision::PacketNotifier *notify = frc971::vision::PacketNotifier::MMap(
110 ::aos::camera::Buffers::kHeight * ::aos::camera::Buffers::kWidth * 3);
111 pid_t pid = fork();
112 if(pid < 0){
113 fprintf(stderr,"%s:%d: Error in fork()\n",__FILE__,__LINE__);
114 }
115 if(pid == 0){
116 notify->RegisterSender();
117 sender_main(notify);
118 }else{
119 notify->RegisterReciever();
120 reciever_main(notify);
121 }
122}
123