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