$ cat cv.cpp #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream>
#include <wiringPi.h>
#define MIN_POS 30 #define MAX_POS 110
using namespace cv; using namespace std;
void init_wiringpi() { if(wiringPiSetup() == -1) exit(0);
pinMode(1, PWM_OUTPUT); pwmSetMode(PWM_MODE_MS); // pwmSetRange(1024); // pwmSetClock(400);
pinMode(23, PWM_OUTPUT); pwmSetMode(PWM_MODE_MS); pwmSetRange(1024); pwmSetClock(400);
pwmWrite(1, MIN_POS + (MAX_POS - MIN_POS) / 2); pwmWrite(23, MIN_POS + (MAX_POS - MIN_POS) / 2); }
void setPWM_1(int val) { pwmWrite(23, val); }
void setPWM_2(int val) { pwmWrite(1, val); }
void on_trackbar_1(int pos, void *ptr) { int val = MIN_POS + pos * (MAX_POS - MIN_POS) / 100; setPWM_1(val);
cout << "pos 1 [" << pos << ":" << val << "]" << endl; }
void on_trackbar_2(int pos, void *ptr) { int val = MIN_POS + pos * (MAX_POS - MIN_POS) / 100; setPWM_2(val);
cout << "pos 2 [" << pos << ":" << val << "]" << endl; }
void on_trackbar_3(int pos, void *ptr) { int val = MIN_POS + pos * (MAX_POS - MIN_POS) / 100; // setPWM_1(val); // setPWM_2(val);
setTrackbarPos("track 1", "cam 1", pos); setTrackbarPos("track 2", "cam 2", pos);
cout << "pos 3 [" << pos << ":" << val << "]" << endl; }
int main(int argc, char** argv) { // opencv 2 style VideoCapture cap(0); int pos[2];
init_wiringpi();
if(!cap.isOpened()) { cout << "No camera detected" << endl; return -1; } else { cout << "In capture ..." << endl; cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); }
VideoCapture cap2(1); if(!cap2.isOpened()) { cout << "No camera detected" << endl; return -1; } else { cout << "In capture ..." << endl; cap2.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 240); }
namedWindow("cam 1", WINDOW_AUTOSIZE ); namedWindow("cam 2", WINDOW_AUTOSIZE ); namedWindow("control", WINDOW_AUTOSIZE );
createTrackbar("track 1", "cam 1", &pos[0], 100, on_trackbar_1 ); createTrackbar("track 2", "cam 2", &pos[1], 100, on_trackbar_2 ); createTrackbar("track 3", "control", &pos[0], 100, on_trackbar_3 );
setTrackbarPos("track 1", "cam 1", 50); setTrackbarPos("track 2", "cam 2", 50); setTrackbarPos("track 3", "control", 50);
for(;;) { Mat frame; if(!cap.read(frame)) break; imshow("cam 1", frame);
if(!cap2.read(frame)) break; imshow("cam 2", frame);
if(waitKey(30) >= 0) break; }
return 0; } |