South Africa
Asked — Edited

Move Servos According To Camera Coordinates

Hi everyone,

I've been playing around with the color tracking on my JD and while I can get the tracking to work, I'm having trouble getting the servos to move in response to seeing a color. Here is the code I have been using to try and Identify the color blue and then point in the general direction of the blue object:


:loop

$CameraObjectColor = ("Blue" ) ControlCommand("Camera", CameraStart)

SayWait("let me see" )

ControlCommand("Camera", CameraMultiColorTrackingEnable)

IF ($CameraObjectColor != "Blue" ) SayWait(" No I do not see the color" ) ENDIF

IF ($CameraObjectColor = "Blue" ) SayWait("I see a dot colored" + $CameraObjectColor) SayWait("It is in" +$CameraVerticalQuadrant +"and" + $CameraHorizontalQuadrant) ENDIF IF($CameraHorizontalQuadrant < 0) Servo(D4, 110) IF($CameraVerticalQuadrant < 0) Servo(D3, 75) ENDIF IF($CameraVerticalQuadrant > 0) Servo(D3, 110) ENDIF ENDIF IF($CameraHorizontalQuadrant < 0) Servo(D7, 110) IF($CameraVerticalQuadrant < 0) Servo(D2, 75) ENDIF IF($CameraVerticalQuadrant > 0) Servo(D2, 110) ENDIF ENDIF goto (loop)


I have not been able to get the servos to move at all. Can anyone suggest what I might be doing wrong? Thank you :)


ARC Pro

Upgrade to ARC Pro

With ARC Pro, your robot is not just a machine; it's your creative partner in the journey of technological exploration.

#1  

Maybe this will work....? You'll need a lot of light on the object and check different shades of blue as well....




$CameraObjectColor = &quot;Nil&quot; 
ControlCommand(&quot;Camera&quot;, CameraStart)

SayWait(&quot;let me see&quot; )

ControlCommand(&quot;Camera&quot;, CameraMultiColorTrackingEnable)
sleep(3000) #give tracking a chance to initialize
repeatuntil(1=2)

If($CameraIsTracking = 1) #check to see if the camera is actually tracking

IF ($CameraObjectColor != &quot;Blue&quot; )
 SayWait(&quot; No I do not see the color&quot; )
ENDIF 

IF ($CameraObjectColor = &quot;Blue&quot; )
 SayWait(&quot;I see a dot colored&quot; + $CameraObjectColor)
 SayWait(&quot;It is in&quot; +$CameraVerticalQuadrant +&quot;and&quot; + $CameraHorizontalQuadrant) 
ENDIF
IF($CameraHorizontalQuadrant &lt; 0)
 Servo(D4, 110)
 IF($CameraVerticalQuadrant &lt; 0)
 Servo(D3, 75)
 ENDIF
 IF($CameraVerticalQuadrant &gt; 0)
 Servo(D3, 110)
 ENDIF
ENDIF
IF($CameraHorizontalQuadrant &lt; 0)
 Servo(D7, 110)
 IF($CameraVerticalQuadrant &lt; 0)
 Servo(D2, 75)
 ENDIF
 IF($CameraVerticalQuadrant &gt; 0)
 Servo(D2, 110)
 ENDIF
ENDIF
ENDIF
sleep(1000) #slow the loop down and give a chance to see a colour
endrepeatuntil

United Kingdom
#2  

Not an answer to your question but an observation which may lead to more efficient code and easier to follow code...

ElseIf and Else can be used to save nesting Ifs inside Ifs. Also, or and and operators can also be used to make If statements easier to follow.


$CameraObjectColor = (&quot;Blue&quot; )    # Should this be an IF? This line currently sets the variable $CameraObjectColor to &quot;Blue&quot;.

The rest of the code can just be simplified with ElseIf and Else to eliminate the nested Ifs. This should then help finding the problems and any bugs you may have.

#3  

Why not use servo relative tracking? The code seems to be doing the same thing...

#4  

@Technopro Maybe the op has additional code he wants to add to the script in order to do something custom?

United Kingdom
#5  

Or they may be like me and want to script everything regardless of if there is a control for it.

I ment to add, if you need some guidance on the ifs, elseifs and elses or the structuring of code to make it easier to follow (and therefore easier to adjust 3 years down the line) I'll gladly re-write it with comments and explanations. Personally I am very picky when it comes to code and, at least my own or any I am using needs to be structured well, please don't take offence if this is implying I think your code isn't well structured (blame my OCD) :)

#6  

Hi everyone, I'm very sorry for not replying, I had some trouble with my email with is why my user name is a bit different now. Thank you very much for all your suggestions I will get to work trying them out right away and give you some feedback. Yes, I am trying to code all of it instead of using controls. @Rich, the sleep commands work very well, thank you! I didn't quite understand how to use them properly till that

#7  

@Rich I'm not sure I fully understand the repeat until statement in your code, I know it is meant to repeat all the code between RepeatUntil and EndRepeatUntil until the specified condition is met but I don't understand the condition 1=2?

United Kingdom
#8  

@RichardR uses an impossible statement to repeat forever. It's no different to using the label and goto method.

#9  

I put the repeatuntil in you code so the script will keep executing... meaning it will keep looking for blue, otherwise it will look just once and then quit... Repeatuntil(1=2) just means that this condition will never be met so the repeatuntil loop will keep looping ... that's all that means

@Prenil01 Did you have the goto loop in your code when you first posted it? If you did, I missed that, sorry... Then you don't need the repeatuntil command....

PRO
Synthiam
#10  

Why not enable servo tracking instead of writing code?

#11  

@RichardR It was in my code but I appreciate you adding that anyway. It taught me something I didn't know before so thank you for that !

@Rich to your previous comment: No offense taken at all- I welcome any constructive criticism. If it isn't too much of a bother for you, I'd really appreciate you showing me how to improve my coding.

@DJ_Sures I enjoy being able to code it in a script. However I've had more free time to work on this recently and I will definitely try the interface soon as well. Just one question about that to make sure I understand properly. Would it be correct to say that enabling servo tracking allows any servo to be moved by a predetermined amount when tracking an object and tracking by relative position moves the robot to keep the object in the middle of the camera assuming that the camera can't move?