Shop OBEX P1 Docs P2 Docs Learn Events
Stepper Pan and Tilt — Parallax Forums

Stepper Pan and Tilt

zwdaviszwdavis Posts: 8
edited 2013-04-06 14:05 in Propeller 1
I've been developing a pan and tilt camera mount on the side for some time now. I finally have everything built and all of my hardware, both electrical and mechanical, has been tested and proven. Now on to the fun part of spending years debugging...
The problem that I am having is translating my speed determined from the joystick to the objects driving each motor. My current code simply relaunches the code into the same cog every time through the repeat loop with the updated speed value. I know that my individual objects do what they are supposed to and that the speed values I am calculating are right I just need to find a better way to pass this speed value to the launched cog without re-initializing it every time. Currently whenever I move the joystick to a certain position the corresponding motor will vibrate. I feel this is due to the fact that I am going through the repeat loop so fast that the cog just keeps getting re-initialized and that its not having enough time to actually run the motor. Adding a waitcnt command to the end of the repeat loop with varying time values from .1 seconds to 1 seconds makes the code not work at all.
I have included all of my code here...
pantilt.zip
The motorsuccess file is one that moves the platform continuously and is the basis for my motor objects in the main file.
This is my first project with the propeller so needless to say my code is messy and confusing. Any help anyone can offer would be greatly appreciated.

Thanks!
Zach

Comments

  • SRLMSRLM Posts: 5,045
    edited 2013-03-29 19:53
    Some thoughts:
    - Watch out for your indentation. I notice that you have a repeat an column 0.
    - Have you looked at the available PWM objects out there? Those are already tested and ready to go.

    It's probably a bad idea to relaunch a cog doing PWM. If you do that, then there will be a glitch every time it is launched since a) it takes time to launch, and b) the new instance does not remember where in the PWM cycle it is. A better solution is to use a shared global variable that both the main thread and the botmotor cog thread can read.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-03-29 21:29
    Your code is somewhat messy, and this is one of the easiest ways to introduce bugs. Keep things neat and orderly and you'll create fewer bugs; when you do, they'll be easier to find and fix in a neatly-formatted listing.

    I've attached a simple dual-motor object that I wrote for my Nuts & Volts column. A slightly advanced version of this code (which uses encoder feedback for speed and position) is used in this product (produced by one of my friends).

    -- http://www.cameraturret.com/genesis.htm
  • zwdaviszwdavis Posts: 8
    edited 2013-03-30 21:20
    SRLM-
    The global variable approach is where I knew I needed to head but didn't yet know how. I found an example that I tried to adapt of using this technique and have changed my code accordingly. Now my motors lock up and don't respond to the joystick at all. This means that a constant signal is going to my drivers and it is not getting pulsed. The pulsing I am using in this object was adapted from an example in the PE kit labs and if I hard code any of my possible speed values into the object and load it to the pan and tilt system the motors turn as expected. Any ideas on why my global variables aren't working as I thought they would with the way my code is currently written?

    Mr McPhalen-
    I read through the code you posted and don't feel any more clear on how to fix my problem. Any suggestions on how to fix my global variable approach would be greatly appreciated.

    pantilt.zip

    Thanks,
    Zach
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-03-30 22:30
    I don't understand what the problem is. You say you can calculate speed -- my object will take speed (-100 to 100 percent) and run your motors without resorting to restarting cogs. Your foreground loop should read and filter the inputs a reasonable rate.

    I would gently suggest that the architecture of your code is at the root of your problems.

    [ Edit ] I whipped up a simple program (compiles, but not tested -- you'll need to adjust for your circuit) that should get you moving in the right direction. The biggest issue, I believe, is the constant starting and stopping of motor cogs. You have two motors. It's very convenient that each cog has two counters and my object uses one each for a motor -- you never have to shutdown the motor cog, you just update each channel (0 or 1) with new speed.

    The main loop reads the ADC and takes the a running average of the last eight readings (adjust with SAMPLES constant). This is scaled to speed (-100 to +100) -- I'm working on the assumption that the center point of your joystick will be center of the ADC range.

    This is a very simple program. In my friend's commercial application we read the joystick on start-up to adjust for the neutral position, we have a dead zone for stopping, there is another channel that affects the allowable rate-of-change, etc. Still, the core is demonstrated in the attached program and you should be able to take advantage of it.
  • zwdaviszwdavis Posts: 8
    edited 2013-04-06 14:05
    Mr McPhalen-
    I cannot thank you enough for your help with this. I downloaded your code and have been testing and trying to understand it for some time now. I had to make some modifications to the scale method to get it to properly calculate the speed from the joystick reading but it is doing that correctly now. However, my motors still aren't responding to changes in the joystick. I'm having a hard time with the PWM side of things in that I was only able to get my old motor code to work by trial and error. So i guess where I'm at now is trying to convert my pulse speeds from my old code to the new code. Any more suggestions you can give me?

    Thanks again,
    Zach
Sign In or Register to comment.