BASIC program for robot

Discussion in 'Programmer's Corner' started by tamuzik, Jun 17, 2009.

  1. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    I'm working on a computer interfaced robotic arm. I need basic program to control my stepper motors. I av written a code but it doesn't seem to work. Pls I need help. Its urgent
     
  2. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    why don't you include some of the code you have written and tell us what does and does not seem to be working?
     
  3. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    It would be nice to have the schematic for the connections to the stepper motor, too.
     
  4. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    I will send the schematic diagram as well as my code soonest. Thanks so much
     
  5. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    Im having problem posting the schematic diagram
    The ULN2003 IC will control the motor, depending on the output from the PIC. My problem, is my program is just moving the motor continously. I need a program that can stop the moter whenever I want it to . E.g if I want a movement of 90 degrees it should stop at 90. This is what I was able to write for the ULNs connected to port d.
    program robot
    main:
    trisd = %00000000
    while true
    if portb = %00000001 then
    portd = %00000011 'the initial state of the motor

    delay_ms(500)
    portd = %00000110 'a step movement

    delay_ms(500)
    portd = %00001100
    delay_ms(500)
    portd = %00001001
    delay_ms(500
    else if portb = %00000010 then
    portd = %00110000
    delay_ms(500)
    portd = %01100000
    delay_ms(500)
    portd = %11000000
    delay_ms(500)
    portd = %10010000
    delay_ms(500)
    else portd = %00000000
    end if
    end if
    wend

    end.
     
  6. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    Pls find attached the schematic diagram of the arm
     
  7. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    On the schematic, I think you need to change the configuration of the switches on portB. Put the resistors to from the RB0 and RB1 pins to GND. That way you will have a definite state for the input pins when the switches are not engaged. Right now they are just floating...

    You may also find that you need to debounce the switches so you have more precise control. Here is a link on debouncing switches that someone gave in another thread.

    http://www.ganssle.com/debouncing.pdf
     
  8. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    Thanks.I will change the configuration of the switch as specified. I tried opening http://www.ganssle.com/debouncing.pdf
    but it did not open.

    Any correction on my program?
    Thanks so much
     
  9. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    Try changing the switch setup and resistors and then report back on what the circuit is doing. You said that your motor was moving so the control may be correct. Allowing the resistors to pull down the input to '0' may be enough to get you out of the loop that is continuously moving the motor.

    Try googling switch debounce to get additional information. The link, that does not work for you, is nice because it gives hardware and software methods along with the general discussion. I don't know why you can't view it. Maybe try right click on the mouse and choose "Save Link As" option.
     
  10. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Either you do not have Adobe Reader installed, or you have a very slow Internet connection.

    If you do not have Adobe Reader installed, you can download it from here:
    http://get.adobe.com/reader/
    It is a large file, around 25.5MB.
     
  11. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
  12. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    One thing I noticed about your program is that you did not initialize portb to be an input.

    Another is that you are spending a lot of time outputting data to ports, and then waiting.

    Try this code:
    ---------------------------------------------------------------------
    program Stepper_proj
    dim idl, idh as byte ' indexes for portd low & high nybbls
    dim statel as byte[5] ' Storage for states; low nybbls
    dim stateh as byte[5] ' Storage for states; high nybbls

    main:
    trisb = %11111111 ' Portb is input
    trisd = %00000000 ' Portd is output

    statel[1]= %00000011 ' Initialize low nybbl array
    statel[2]= %00000110
    statel[3]= %00001100
    statel[4]= %00001001

    stateh[1]= %00110000 ' Initialize high nybbl array
    stateh[2]= %01100000
    stateh[3]= %11000000
    stateh[4]= %10010000

    idl = 1 ' Low nybbl index for portd
    idh = 1 ' High nybbl index for portd
    portd = statel[idl] or stateh[idh] ' Initialize portd state to %00110011
    ' %00000011 or
    ' %00110000 =
    ' %00110011

    while true ' Main loop
    if portb and %00000001 then ' If port bit is high, increment idl
    if idl < 4 then '... if less than 4...
    inc(idl)
    else
    idl = 1 '... otherwise, reset index to 1.
    end if '... resulting in 2, 3, 4, 1, 2 ...
    end if

    if portb and %00000010 then ' If port bit is high, increment idh
    if idh < 4 then '... if idh less than 4...
    inc(idh)
    else
    idh = 1 '... otherwise, reset index to 1.
    end if '... resulting in 2, 3, 4, 1, 2 ...
    end if
    portd = statel[idl] or stateh[idh] ' Set port to new state
    delay_ms(100) ' Delay 100mS (1/10 second) per step.
    wend
    end.
    ---------------------------------------------------------------------
    Note that if you click the "Quote" button below, you will be able to copy the source code with it's original formatting intact, and paste it into the mikrobasic editor.

    [eta]
    Note that this code will cause the motor(s) attached to portd to step as long as one (or both) of the buttons attached to portb are pressed, pulling the input pin(s) high.

    There are no provisions to turn the robot a specific number of degrees. More information would be required, such as the number of steps per complete revolution of the stepper motor, diameter of the wheels, the distance between the wheels, etc. Even with such information, the number of degrees turned would not be precise, as there will be some "slippage" between the wheels and the traction surface.
     
    Last edited: Jun 27, 2009
  13. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    This will help you with your switch inputs:
    [​IMG]

    R2 keeps the PIC input low by discharging C1 when S1 is not closed.
    R1 charges C1 when S1 is closed. It also limits the current through S1 so that the switch contacts will last longer.
     
  14. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    I changed the switch configuration, connected to ground though it indicates off when I switch it off but it's still working the same way.
    About driving the motor using nibbles, do you know a better alternative?
    I tried
    http://techni.caliti.es/blog/2008/11...ontroller.htm
    but it doesn't open. I dont knw why Im aving the problem
     
  15. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    That really did not make any sense. :confused:
    Why don't you post a schematic of how you currently have the inputs connected?

    You probably don't have a .ZIP file decompression utility installed.
    You could look at the raw source code from this post:
    http://www.picbasic.co.uk/forum/showpost.php?p=69710&postcount=12
    however, the code was written for a different Basic compiler, a different PIC uC, and a different hardware layout. A fair bit of the program may have to be re-written; offhand I don't know how much that would involve.

    I threw the above program (post #13) together fairly quickly using portions of the code you'd already written, and added the state arrays for the high nybbles/low nybbles and ORing them to get the port outputs. I kept it short so that it wouldn't be terribly difficult to understand. I happened to have a demo of mikrobasic installed, and my example compiles with no errors reported.

    Arrays and subscripting can be pretty confusing at first. So can bitwise AND and OR operations. But, the way I combined those items makes the resulting code a lot shorter.
     
  16. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    Waoh! Thanks so much. I have used the code and it's working perfectly well for the first motor while the second motor keeps running without control. Am still studying the code to see if I can make necessary corrections.
    I no longer have problem with the switch. I used the circuit you gave and it worked perfectly fine.
    I was able to open the stepper_driver using the link you gave. It looks complicated somehow but I will still study it for proper understanding.
    THANKS EVERYONE
     
  17. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    That's odd. Are you certain that the portb input control for that stepper is going near 0v when the pushbutton is released? If it's not, check to make certain that R2 is connected properly, and that the switch is not shorted.

    I just reviewed the code again, and I don't see any errors in it.
    If you want to just test one part of the IF statements, then you could change:
    if portb and %00000001 then
    or:
    if portb and %00000010 then
    to:
    if portb and %00000000 then
    temporarily.
    It seems that you ARE still having a problem with one of the switches.

    The stepper driver in that link has quite a few more features than the simple one I posted, but will need a lot of changes to work with your design, PIC and compiler.

    One problem with the current design that you will run into right away is running out of I/O pins. You currently only have controls to run two of the five motors, and in only one direction. You will either need to use a serial interface for control input, reduce the number of stepper motors, or you will need a different scheme for driving the stepper motors.
     
  18. tamuzik

    Thread Starter New Member

    Jun 16, 2009
    8
    0
    Ok, am still on it
     
Loading...