Dear all,
Just about a small investigation from my part.
How the Jetson TX1 could pilot pan & tilt servos through OpenCV.
If you are interested, this work is based on the lib made by Jetsonhacks:
https://www.jetsonhacks.com/2015/10/14/pwm-servo-driver-board-nvidia-jetson-tk1/
This soft permit to a mouse to pilot servos through the video flow of an USB webcam connected to the Jetson TX1.
First, check you have the PCA9685 breakout board:
https://www.adafruit.com/product/815
Second, connect the PCA9685 as follow on the J21 Header connector:
PCA9685 → TX1 (J21 header)
+Vcc → J21: pin 2 (+5V)
GND → J21: pin 3
SDA → J21: pin 27
SCL → J21: pin 28
Third, connect the servo on the PCA9685 breakout board
Servo 1 → Pan → row 0
Servo 2 → Tilt → row 1
Then download the lib from JestonHack as mentioned below.
Download also the lib I2C like this:
sudo apt-get install libi2c-dev
Once it’s done, create on the Jetson TX1 a folder named: testOpeCV
Paste the 2 sources files and the example file of the Jetsonhacks lib.
You will have this :
Directory: testOpenCV
Files within the directory:
JHPWMPCA9685.cpp
JHPWMPCA9685.h
servoExample.cpp
Then modify the servoExample as follow:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <JHPWMPCA9685.h>
using namespace std;
using namespace cv;
/* global variables - MouseHandler */
int pan = 90; //row 0 PCA breakout board
int tilt = 90; //row 1 PCA brekout board
Mat frame;
int mapping(int, int, int, int, int);
void moveMouse(int, int, int, int, void*);
int main()
{
/* values set for a common servo */
int servoMin = 140 ;
int servoMax = 590 ;
VideoCapture cap(CAP_ANY); // just for USB cam
PCA9685 *pca9685 = new PCA9685() ;
pca9685->openPCA9685();
pca9685->setAllPWM(0,0) ;
pca9685->reset() ;
pca9685->setPWMFrequency(60) ;
while(true)
{
cap >> frame;
setMouseCallback("Frame", moveMouse, NULL);
/* mapping to follow the screen resolution (640x480) */
pca9685->setPWM(0,0,map(pan,0,640,servoMin, servoMax));
pca9685->setPWM(1,0,map(tilt,0,480,servoMin, servoMax));
imshow("Frame", frame);
waitKey(30);
}
destroyAllWindows();
pca9685->closePCA9685();
return 0;
}
int mapping(int x, int in_min, int in_max, int out_min, int out_max)
{
int toReturn = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min ;
return toReturn ;
}
void moveMouse(int event, int x, int y, int flags, void* userdata)
{
if(event == EVENT_MOUSEMOVE)
{
pan = x;
tilt = y;
}
}
Save the code, and compile like this (Always on the Jetson TX1):
g++ servoExample.cpp JHPWMPCA9685.cpp - o servoExample `pkg-config --cflags --libs opencv` -lopencv_core -lopencv_highgui -lopencv_video -lopencv_videoio -lopencv_imgproc
Don’t forget to plug the power to the breakout board (6Vcc) ;-)
then, launch the soft with the root access:
sudo ./servoExample
Now you can pilot the webcam with the mouse…enjoy !
Christophe