loading
 
Arduino Motor Control
Anonymous
2 years
Hi, I am working on object tracking, similar to the tutorial Color tracking! But the code written there is for non arduino board. I want to develop the same using arduino and which is the preferrable motor driver to be used?  
And Can i have a sample of how the VBscript program and arduino code must be?

Thanks in advance!!
Steven Gentner from United States  [1370 posts] 2 years
You can't process images on the Arduino without additional hardware ... the code was written to work on a PC (there is a PC running on that robot).

You may instead want to research other hardware boards that one can use with the Arduino to do what you are asking about. We don't have anything like that. If you have a PC that can be used to communicate to an Arduino just to move motors/servos/etc. but you still need a PC in the loop.

STeven.
Anonymous 2 years
Thanks steven!! Yes i do have a PC and i ulpoaded the uno sketch!! I am also now able to get the centre of gravity of image detected and send the values to servo motor, using sparkfun arduino module. I have established the serial communication.
All i need is,

  ' get the current center of the image
  midx = GetVariable("IMAGE_WIDTH") / 2
  midy = GetVariable("IMAGE_HEIGHT") / 2
  
  ' set the movement neutral range, i.e. if the
  ' cog is with range of the center of the image
  ' then consider it ok and move straigh. This
  ' helps to prevent fine tune movements that
  ' can cause oscillations.
  range = 5
  
  ' determine the factor that scales image width
  ' to a range of 0 to 128 .. increase beyond 128
  ' if your robot turns too quickly
  horizFactor = 200 / midx
  
  ' vertical factor ... we don't have much
  ' range here so the number is larger than
  ' horizontal movement
  vertFactor = 300 / midy
  
  ' ball size when it is too close
  ballSize = 40

  ' initialize our start motor values to neutral
  left_base = 128
  right_base = 128

  ' get the size (width or height) of the current
  ' bounding box
  size = GetVariable("COG_BOX_SIZE")

  ' if it is equal to "" then no object was detected
  if size > 10 then

    ' get the horizontal center of gravity found by the COG
    ' module
    cogX = GetVariable("COG_X")
    ' if it is less than 75 the blob is on the left side
    ' of the screen so move that way
    if cogX < (midx - range) then
      left_base = 128-(midx-cogX)/horizFactor
      right_base = 128
    ' otherwise move to the right if above 85 pixels (and no
    ' movement if 75 < cogX < 85 )
    elseif cogX > (midx + range) then
      left_base = 128
      right_base = 128-(cogX-midx)/horizFactor
    end if

    ' if the ball is too close then we have to move backwards
    if size > ballSize then
      left_motor = left_base+((size-ballSize)*2)
      right_motor = right_base+((size-ballSize)*2)
    ' otherwise move forward
    else
      left_motor = left_base-((ballSize-size)*2)
      right_motor = right_base-((ballSize-size)*2)
    end if

    ' set the final motor values to be picked up by
    ' the SSC module
    SetVariable "LEFT_MOTOR", CInt(left_motor)
    SetVariable "RIGHT_MOTOR", Cint(right_motor)

    ' now lets work on the tilt ... grab the current
    ' tilt value
    tilt = GetVariable("TILT")
    ' if it was not set then default to 220 (in our case
    ' this is horizontal to the floor)
    if tilt = "" then
        tilt = 220
    end if

    ' if the blob is below 55 then tilt down up a bit more
    ' otherwise tilt up a little more
    cogY = GetVariable("COG_Y")
    if cogY < midy - range then
        tilt = tilt - (midy-cogY)/vertFactor
    elseif cogY > midy + range then
        tilt = tilt + (cogY-midy)/vertFactor
    end if

    ' we don't want to look up more than horizontal to the
    ' floor as we do not expect the
    ' ball to be on the ceiling
    if tilt > 220 then
      tilt = 220
    end if
    if tilt < 0 then
      tilt = 0
    end if

    ' and set that value for the SSC module too
    SetVariable "TILT", tilt
  
  else

    SetVariable "LEFT_MOTOR", 128
    SetVariable "RIGHT_MOTOR", 128

  end if

this code cannot be used for arduino, i want a similar code that can run through arduino and control the dc motors
Steven Gentner from United States  [1370 posts] 2 years
Just for clarification, you are adding this to the current UNO sketch that you uploaded or is this to be standalone? I.e. you are instead sending just the COG_X and COG_Y to the Arduino and it should figure out how to create the motor and pan/tilt values?

I'm just trying to understand why you would want to do this before we spend unnecessary time converting VBscript to Processing.

STeven.
Anonymous 2 years
OK let me be clear again!! the uno sketch that's given in the spark fun_arduino module is uploaded in uno!! I did the the image processing part and got COG_X and COG_Y  values!! Now I want drive 4 motors/ thrusters,  2 forward / reverse, 2 upward / down. Where and how do I set the motor speed? and how to generate a VBScript that can interface with arduino!
Steven Gentner from United States  [1370 posts] 2 years
The VBScript interfaces to the Arduino through the use of variables. Once you set a variable in VBScript using SetVariable it becomes available in the Arduino GUI module on the PC in one of the menu dropdowns (that's what that variable list is for in that interface). Thus, if you look at the menu next to one of your PWM pins you will see that variable created by the VBScript module (and many others) and can select it. When you do that, the value in that variable gets sent to the Arduino which then will set the value of pins accordingly.

So in fact, you don't need the VBScript to run on the Arduino since it is running on the PC and the results of that just get passed to the Arduino via its module.

STeven.
Anonymous 2 years
Thanks Steven!sorry for the late reply!! will i be able to control six thruster motors, through what you said?
Steven Gentner from United States  [1370 posts] 2 years
Yes, you would need 6 variables and an understanding of what motors need to do what in order to move as expected. I.e. in order to move forward motors 1 and 2 would be set to 100 (assuming 100 is forward) and all others would be neutral (0). Assuming that the corresponding variables would be

motor1 = 100
motor2 = 100
motor3 = 0
motor4 = 0
motor5 = 0
motor6 = 0

. You'd then place each of these variables in the appropriate pin slots in the Arduino module (i.e. the dropdown menu) and figure out what variable needs what value in the VBScript module.

As your current VBScript is meant for mobile pan/tilt robot, you probably want to map the joystick differently to create a more logical layout ... unless you want to control all thrusters directly with the joystick in which case that would be more a direct mapping. There is a lot to think about when designing this kind of control system. Its not hard to do mathematically ... its more difficult to figure out what 'feels' like a natural control system, i.e. what parts of the joystick do what.

STeven.
Anonymous 2 years
Wow this is was really helpful!! Ok i need to get the PWM signals to the thrusters!! How do i that?
The Logic I am working on is, I do all the Image Processing and then I get the COGX and COGY values. Through these values i need to get forward/backward or up/down movements. I will attach my .robo file, could you please have look into that, help me out with the exact modification i need to make?
Thanks a lot again in advance.  

program.robo
Steven Gentner from United States  [1370 posts] 2 years
We can't really create a robofile that just 'works' without having physical access to your system. There are just too many unknowns for us to perfectly predict how this will work.

What you need to do is understand how data flows in RR. There are 2 ways that modules communicate with each other, one is via the image (which isn't relevant in your case) and the other is through variables. Variables are created and used in modules but you NEED to tell modules which variables to use. Modules know which variables to create (that's typically hardcoded into some like COG_X) but you need to specify those variables in other modules in order from them to know about that bit of data.

You can think of it like a wiring diagram, where the wires are labelled ... those labels are what we can variables. They are contains of information that are created and consumed by modules in various custom ways. Because we cannot think of all the possible circumstances on how modules will be linked we have to create modules that one can type in variables into specific areas.

For example, in you case where you want to control a thruster using a joystick. The joystick module will ask you for a variable (we normally use JOY_X) which will be populated by one of the joystick parts (again it depends on which stick you want to use for what) and have a value that ranges from -1000 to 1000 depending on the position of the stick.

Now, the PWM field in the Arduino module requires 500 to 2500 so we can't just simply stick the JOY_X in that variable field since the value range is wrong ... well, you can, you will just not get the full range you want. Only the overlap will work, i.e. 500 to 1000.

So the trick is to scale the JOY_X to the correct range .. this is incidentally what the differential module does ... nothing complicated here. This is how its done:

add 1500 to the JOY_X variable causes the range to become -1000+1500 to 1000+1500 ... or surprise .. 500 to 2500!

So you can simply use one of the script module or even the Set_Variable module with

my_pwm_value = [JOY_X+1500]

and then stick 'my_pwm_value' which is now a variable that responds to the joystick but in the range 500 to 2500 and place that in the appropriate pin variable field in the Arduino.

Try just this to get the idea. All other controls flow from this same principal.

Make sense?

STeven.
 [29 posts] 2 years
I was able to program both ball and a bucket tracking system.
Look at my youtube "Scruffman2001" and watch the "GardenBot" and Ball Tracker video.  let me know It this is what you need.
Doyle
Anonymous 2 years
hi maleche!! Thanks for replying, well that's more or less what i need. When the camera detects the object it must send a command to the forward motors and vehicle must go front.

Will it be fine if you can attach your robo file, i can refer to it!!?

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index