blob: f17803e745b9c8a385707bb34e4015ec065ce123 [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>
Brian Silverman6ae77dd2013-03-29 22:28:08 -07007#include <netinet/in.h>
8#include <netinet/tcp.h>
9#include <arpa/inet.h>
Brian Silverman6ae77dd2013-03-29 22:28:08 -070010
Brian Silverman6742a442013-11-03 12:58:42 -080011#include <vector>
12#include <iostream>
13
Brian Silverman6ae77dd2013-03-29 22:28:08 -070014#include "aos/common/time.h"
15#include "aos/atom_code/camera/Buffers.h"
16#include "aos/externals/libjpeg/include/jpeglib.h"
Brian Silverman5b3e51e2013-03-29 22:53:44 -070017#include "aos/atom_code/init.h"
Austin Schuh86bec782013-04-04 05:50:52 +000018#include "aos/common/logging/logging.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -070019
20#include "vision/OpenCVWorkTask.h"
21#include "vision/CameraProcessor.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -070022#include "vision/JPEGRoutines.h"
23
24
Brian Silverman6ae77dd2013-03-29 22:28:08 -070025namespace frc971 {
26namespace vision {
27
28} // namespace vision
29} // namespace frc971
30
Austin Schuh86bec782013-04-04 05:50:52 +000031namespace {
32void SaveImageToFile(IplImage *image, const char *filename) {
33 FILE *file = fopen(filename, "w");
34
35 fputs("P3\n320 240\n255\n", file);
36 ::cv::Mat img(image);
37 for (int i = 0; i < img.rows; i++) {
38 for (int j = 0; j < img.cols; j++) {
39 // You can now access the pixel value with cv::Vec3b
40 fprintf(file, "%d %d %d ",
41 img.at<cv::Vec3b>(i,j)[0],
42 img.at<cv::Vec3b>(i,j)[1],
43 img.at<cv::Vec3b>(i,j)[2]);
44
45 }
46 fputs("\n", file);
47 }
48 fclose(file);
49}
50}
51
Brian Silverman6ae77dd2013-03-29 22:28:08 -070052#include "frc971/queues/CameraTarget.q.h"
53using frc971::vision::targets;
54
Brian Silverman6742a442013-11-03 12:58:42 -080055void sender_main(){
Brian Silverman6ae77dd2013-03-29 22:28:08 -070056 ::aos::camera::Buffers buffers;
57 CvSize size;
58 size.width = ::aos::camera::Buffers::Buffers::kWidth;
59 size.height = ::aos::camera::Buffers::Buffers::kHeight;
Brian Silverman6742a442013-11-03 12:58:42 -080060 unsigned char buffer[::aos::camera::Buffers::Buffers::kWidth *
61 ::aos::camera::Buffers::Buffers::kHeight * 3];
Brian Silverman6ae77dd2013-03-29 22:28:08 -070062
63 // create processing object
64 ProcessorData processor(size.width, size.height, false);
65
66 printf("started sender main\n");
Austin Schuh86bec782013-04-04 05:50:52 +000067 LOG(INFO, "Camera server started\n");
Brian Silverman6ae77dd2013-03-29 22:28:08 -070068 while(true){
69 //usleep(7500);
70 size_t data_size;
71 timeval timestamp_timeval;
Brian Silverman6742a442013-11-03 12:58:42 -080072 LOG(DEBUG, "getting new image\n");
Brian Silverman6ae77dd2013-03-29 22:28:08 -070073 const void *image = buffers.GetNext(
Austin Schuh86bec782013-04-04 05:50:52 +000074 true, &data_size, &timestamp_timeval, NULL);
75 ::aos::time::Time timestamp(timestamp_timeval);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070076
Brian Silverman6742a442013-11-03 12:58:42 -080077 LOG(DEBUG, "Got new image\n");
78 frc971::vision::process_jpeg(
79 buffer, static_cast<unsigned char *>(const_cast<void *>(image)),
80 data_size);
Austin Schuh86bec782013-04-04 05:50:52 +000081
Brian Silverman6ae77dd2013-03-29 22:28:08 -070082 // build the headers on top of the buffer
Austin Schuh86bec782013-04-04 05:50:52 +000083 cvSetData(processor.src_header_image,
84 buffer,
85 ::aos::camera::Buffers::Buffers::kWidth * 3);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070086
Austin Schuh86bec782013-04-04 05:50:52 +000087 // Reset.
88 processor.clear();
89 // transform the buffer into targets
90 processor.threshold(buffer);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070091
Austin Schuh86bec782013-04-04 05:50:52 +000092 processor.getContours();
93 processor.filterToTargets();
94
95 // could be used for debug ie drawContours
96 //std::vector<std::vector<cv::Point> > target_contours;
97 //std::vector<std::vector<cv::Point> > best_contours;
98 std::vector<Target>::iterator target_it;
99 Target *best_target = NULL;
100 // run through the targets
Brian Silverman6742a442013-11-03 12:58:42 -0800101 LOG(DEBUG, "Found %u targets\n", processor.target_list.size());
Austin Schuh86bec782013-04-04 05:50:52 +0000102 for(target_it = processor.target_list.begin();
103 target_it != processor.target_list.end(); target_it++){
104 //target_contours.push_back(*(target_it->this_contour));
105 // select the highest target
106 if (best_target == NULL) {
107 best_target = &*target_it;
108 } else {
109 if (target_it->height < best_target->height) {
110 best_target = &*target_it;
111 }
112 }
113 }
114 // if we found one then send it on
115 if (best_target != NULL) {
Brian Silverman6742a442013-11-03 12:58:42 -0800116 LOG(DEBUG, "Target is %f\n", best_target->rect.centroid.x);
Austin Schuh86bec782013-04-04 05:50:52 +0000117 targets.MakeWithBuilder()
118 .percent_azimuth_off_center(
119 best_target->rect.centroid.y / (double)::aos::camera::Buffers::kHeight - 0.5)
120 .percent_elevation_off_center(
121 best_target->rect.centroid.x / (double)::aos::camera::Buffers::kWidth - 0.5)
122 .timestamp(timestamp.ToNSec())
123 .Send();
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700124 }
Austin Schuh86bec782013-04-04 05:50:52 +0000125 //static int counter = 0;
126 //if (++counter > 2) {
127 //break;
128 //}
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700129 }
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700130}
131
132
133int main(int /*argc*/, char** /*argv*/){
Brian Silverman6742a442013-11-03 12:58:42 -0800134 ::aos::InitNRT();
135 sender_main();
136 ::aos::Cleanup();
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700137}
138