Second MRL tutorial - tracking service with wiring diagram

Services: 

A tutorial that explain how to use tracking service in MRL...

Python Minimal script


#file : service/Tracking.py edit raw
# a minimal tracking script - this will start all peer
# services and attach everything appropriately
# change parameters depending on your pan tilt, pins and
# Arduino details
# all commented code is not necessary but allows custom
# options

port = "COM19"   #change COM port to your own port
xServoPin = 13   #change this to the right servo pin if needed, for inmoov this is right
yServoPin = 12   #change this to the right servo pin if needed, for inmoov this is right

tracker = Runtime.start("tracker", "Tracking")
gui = Runtime.start("gui", "SwingGui")
opencv=tracker.getOpenCV()

# set specifics on each Servo
servoX = tracker.getX()
servoX.setMinMax(30, 150)  #minimum and maximum settings for the X servo
# servoX.setInverted(True) # invert if necessary

servoY = tracker.getY()
servoY.setMinMax(30, 150)  #minimum and maximum settings for the Y servo
# servoY.setInverted(True) # invert if necessary

# changing Pid values change the 
# speed and "jumpyness" of the Servos
pid = tracker.getPID()

# these are default setting
# adjust to make more smooth
# or faster

pid.setPID("x",5.0, 5.0, 0.1)
pid.setPID("y",5.0, 5.0, 0.1)

# optional filter settings
opencv = tracker.getOpenCV()

# not for you, it's for test
#virtual=1
if ('virtual' in globals() and virtual):
  virtualArduino = Runtime.start("virtualArduino", "VirtualArduino")
  virtualArduino.connect(port)
  
# connect to the Arduino ( 0 = camera index )
tracker.connect(port, xServoPin, yServoPin, 0)

if ('virtual' in globals() and virtual):
  opencv.stopCapture()
  opencv.setMinDelay(500)
  opencv.setFrameGrabberType("org.bytedeco.javacv.FFmpegFrameGrabber")
  opencv.setInputSource("file")
  opencv.setInputFileName("resource/OpenCV/testData/monkeyFace.mp4")

gui.undockTab("tracker.opencv")
opencv.broadcastState();
sleep(1)

# Gray & PyramidDown make face tracking
# faster - if you dont like these filters - you
# may remove them before you select a tracking type with
# the following command
# tracker.clearPreFilters()

# diffrent types of tracking

# lkpoint - click in video stream with 
# mouse and it should track
# simple point detection and tracking
# tracker.startLKTracking()
tracker.faceDetect()

# scans for faces - tracks if found
# tracker.findFace()

In this PICTURE I USED PIN 3 for X and PIN 11 for Y -> USE 13 FOR X AND 12 FOR Y INSTEAD !!!!!

 

bstott's picture

Thank You

Thank you for making a tutorial for those of us slow to build.

robrobot's picture

Tracking Service probs

This is a brilliant tutorial and I am excited at the prospect of getting this to work but maybe I am being dumb because I cannot get my servos to move.

Open CV tracking is working fine and I have connected my servos to the arduino as per the diagram. When I upload the sketch the servos twitch but resolutely fail to move when I move the sliders. The board is communicating and the servos are attached with the buttons. I am powering the arduino externally from a wall socket. I am using a 1N4001 diode in the circuit -could this be the problem?

Apologies for asking for help so soon. Any advice would be greatly appreciated.

robrobot

GroG's picture

Welcome roborobot !

Twitchy Servos are typically from evil under-powered sources....

I think its great you got past the uploading sketch, and it seems you have successful communication (from MRL -to-> Arduino)

If it was me, I'd start with an Arduino servo test under examples - just to be sure its all hardware & power problem.

It's been my experience that servos vary greatly on power consumption - and power consumption is not linearly proportional to size.  I've had a few very small & cheap servos consume much more power than large well made servos.

robrobot's picture

Thanks for your help

I will try some different servos tomorrow. Cheers for now

robrobot's picture

nearly there

Thanks so much. Servos are now working after a bit of tweaking. I'm afraid I'm being really dumb now because I cannot find the python script .txt file you used in the tutorial.

GroG's picture

Are the scripts listed above

Are the scripts listed above not working ?

Alessandruino's picture

Tracking.minimal.py tested

Tracking.minimal.py tested and worky for me... Version 1909

googy's picture

Pyton error

Great jobs. Alessandro i need your help. I am going exactly as u describe in this tutorial and i am going step bu step until i reach pyton part. When i try to execute tracking.txt to pyton editor appears the follow error :



and this is the pyton error :



File string, line 2, in moduleAttributeError : org.myrobotlab.service.Tracking object has no Attribute setRestPosition.



whats prblem with pyton pls.



this is the tracking code :



tracker = Runtime.createAndStart("tracker", "Tracking")

tracker.setRestPosition(90,90);

tracker.setSerialPort("COM3");

tracker.setXServoPin(3);

tracker.setYServoPin(11);

tracker.setCameraIndex(1);



tracker.initTracking();

tracker.initControl();

tracker.initInput();



tracker.trackLKPoint();

Alessandruino's picture

Hi googy.. I LL check as soon

Hi googy.. I LL check as soon as i can.. Probably tracking service has changed a little.. So we need to update the script

Alessandruino's picture

Try using tracking.minimal.py

Try using tracking.minimal.py script in this page , modifing the com port and pins if necessary

Alessandruino's picture

Try using tracking.minimal.py

Try using tracking.minimal.py script in this page , modifing the com port and pins if necessary

googy's picture

Works but problem persist !

After i changed to Tracking.minimal.py GUI works. Servos reacts but they are very very slow not in real time as we see on alesssandro`s tutorial. I wana ask some one if my servos could be the problem? i am using small servos from 9g? If i use bigger servos can i get real time reaciton? pls any suggestion...

Alessandruino's picture

Bigger servos are better..

Bigger servos are better.. But reaction can ne regulated increasing Kp values in the PIDs tabs...

Values should be 10 by default.. Try increasing with steps of 5

Ki should be at 0.. I would pur it @ 1.0

Alessandruino's picture

Bigger servos are better..

Bigger servos are better.. But reaction can ne regulated increasing Kp values in the PIDs tabs...

Values should be 10 by default.. Try increasing with steps of 5

Ki should be at 0.. I would pur it @ 1.0

khalednor's picture

please Alessandruino help me

I run the code on the page, but it did not work ... This error appears : (error tracker tracking could not initialize properly) ...What should I do in order to function correctly ...please help me ..

Regards
GroG's picture

You using a very very old

You using a very very old version of MyRobotLab - use the latest..

Steps:

1. make a directory like c:\mrl
2. download myrobotlab.jar and put it in the directory (download from here https://github.com/MyRobotLab/myrobotlab/releases)
3. type the following in a dos shell

cd c:\mrl
java -jar myrobotlab.jar

4. install all the services (runtime->system->install all)
5. restart
6. start python -> runtime -> possible services -> python -> right click -> start -> name it - python
7. try the script again

kaiezer1980's picture

I use N14002 diode instead of

I use N14002 diode instead of the 1N4007

rycarroll92's picture

'150 x limit out of range'

Hi there Alessandruino,

I am tyring to follow your very good tutorial and am having difficulties,

After I run the python script, when tracking in the video screen window I get an error:

"150 x limit out of range"

Is there a reason for this and how do you know how to fix it?

Many thanks

Ryan. 

rycarroll92's picture

Using MRL & Arduino to output DMX

Hi,

I am following this tutorial to make an automated stage lighting luminaire follow an object.

I have a TinkerKit DMX sheild that will output a value from 0 - 255 on any DMX channel. 

What I am trying to do, is substitue the X and Y servos for a DMX channel, so that the automated luminaire can recieve a value, and point the light at the tracking object. (The camera is also mounted to the luminaire).

All I need to do, is take the values for X and Y that are coming in to the arduino, and send them to an arduino DMX shield, it will include them in a DMX packet and send them to the luminaire.

Firstly, I need to understand how MRL sends the data to the arduino for the servos, does it do the calculations for where the servos need to be indexed within the MRL environment. Or does it send the arduino, X and Y values in relation to the tracking service along with the reoslution of the servos for the arduino to calaulate the servo index value?

Any thoughts and guidance on this would be appreciated. Mainly on how to fiddle with the X and Y data for the servos and send it off to the DMX shield. 

The DMX shield has a library that allows you to set the channel value via the serial window.

IE, to set channel 1 to 255, you enter 001c255w

Essentially, I need to replace the value for 001c with the X value for the X servo, and then the Y value for 003c value. 

Many thanks,

Ryan. 

GroG's picture

Hello rycarroll92 and welcome

Hello rycarroll92 and welcome !

Firstly, I need to understand how MRL sends the data to the arduino for the servos, does it do the calculations for where the servos need to be indexed within the MRL environment. Or does it send the arduino, X and Y values in relation to the tracking service along with the reoslution of the servos for the arduino to calaulate the servo index value?

the Tracking services gets the location of the point it is to track in the video stream.  It then runs it through a PID calculation.  The value calculated is sent directly to the servo.  X differences are sent to the pan servo, Y differences are sent to the tilt servo. 

Currently it does not publish the X or Y differences.

In MRL many events are published, and available to other systems or services which subscribe to them.  Its how  the majority of communication is done in MRL (myrobotlab)

...

Reading your comments a little further I see the "consumer" in this case is the same or another Arduino.

...

You could simply hack MRLComm.ino for your own purposes.  If DMX is of interest to us.. we would prefer to "BORG" it in .. and absorb it, so that it becomes just another optional thingy to control.

Humble Regards,

GroG

 

venturehpr's picture

problems

Hello Alessandruino,

 

At moment I'm testing your project, but some problems  has happened. Could you help me?

when I select arduino, the pin and click in attach, not happened. 

the follow status show up:

Could not attach servo to pin 3 serial por is null - not initialized ?

 

 

Alessandruino's picture

Hey venturehpr, if you

Hey venturehpr, if you already have uploaded the arduino script in your arduino board using the arduino ide.. Then you need to start the python script...

Copy the script which is on the top and paste it in the python service tab..then it the execute button

Alessandruino's picture

If you want instead to

If you want instead to manually attach a servo, you got to before connect to the arduino com port writing this code in the python tab :

arduino.connect("Com port name here")

yassinehco's picture

servo sg90 turn slowly

Hello please if someone can tell me why my servo sg-90 when i want to track somthing they turn slowly and turn not exactly to the object sorry for my english

Zack effron's picture

my servo motors are responding to the X and Y testing procedure

they started to response but now they are stopped again. :(
the RX led of arduino is blinking which means that arduino is responding to the command but no further action