PBP projects for R/C models


Closed Thread
Results 1 to 40 of 772

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default No interrupts in my code

    chuck et al,

    My code has no interrupts. It loops through poling the sonars many times per second. They flash a red led when poled. This is visible.

    I use HPWM to generate the controlling signals. This is a stretch. The spec calls for one pulse each 20 msec. With the standard oscillator the PIC can't go that slowly. I learned what works by trial and error.

    When controlling the car using R/C I can make it behave at faster speeds than the PIC can. Why? It might be that the feed back loop through my eyes and fingers is inherently better. My PIC code only works with distances. My eyes and brain work with distance and rate (of closing speed and steering).

    Want to see some code?

    Ken

  2. #2
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default One other thing

    I have been wondering whether the HPWM code starts fresh each time it is called. My system calls it after every SONAR poling. This is many times per second. Could it be that each time HPWM is called it pulls the output pit down in order to create a new pulse stream and in so doing screws up the pulse stream it had been sending.

    Ken

  3. #3
    Join Date
    Nov 2005
    Posts
    51


    Did you find this post helpful? Yes | No

    Default

    Ken yes I would like to see some of your code,
    I'm not sure about the HWPM resetting, If I understand correct the pic is monitoring the distance so when it comes close to the object this decreases the HPWM signal ? to reduce spped and turn the wheels at the same time ?
    Are you sure that the pic to calaculating the distance fast enough to react with the object ?

    I would say that interrupts would be able to act faster than the standard code. it would be nice if you could see how fast the HPWM command is reacting or so fast the pic is processing the data fast enough from the SONAR's

  4. #4
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default Here's some code

    Thanks everyone.

    The speed of PIC calculations can be judged by the rapidity of the flashing LED on the SONARs. My code just goes round and round. This is very visible when I run the car without its skin. It flashes so fast as to hardly be blinking. At least every 100 msec.

    Here's the action part of my code. If you like I can include the constants, pin selections etc.

    main:
    'pause 10 'Assure ringing from previous triggers: are gone.
    GOSUB triggers
    '-------------------
    CheckIfInDanger:
    IF rangefront < frontdanger THEN
    HPWM 1,Straight,50
    HPWM 2,Back,50
    PAUSE 250
    reversing = 1
    GOTO reverseoutoftrouble
    ' loop till this is secured
    ENDIF
    '-------------------
    tryleftturn:
    IF rangefront < frontfree THEN
    'something ahead - turn hard left.
    HPWM 1,Left,50
    HPWM 2,HalfForward,50
    steeringleft = 1
    GOTO main 'loop till this is secured
    ENDIF

    '-------------------
    goingforward:
    HPWM 1,Straight,50
    HPWM 2,Forward,50
    '-------------------
    keepgoingforward: '----Compare to right wall
    IF rangeright > outertrack THEN
    HPWM 1,QuarterRight,50
    ENDIF

    IF rangeright < desiredtrack AND oldrangeright < rangeright THEN
    HPWM 1,Straight,50
    ENDIF

    IF rangeright > desiredtrack AND oldrangeright > rangeright THEN
    HPWM 1,Straight,50
    ENDIF

    IF rangeright > desiredtrack AND oldrangeright < rangeright THEN
    HPWM 1,QuarterRight,50
    ENDIF

    IF rangeright < desiredtrack AND oldrangeright >= rangeright THEN
    HPWM 1,QuarterLeft,50
    ENDIF

    GOTO main
    '-------------------------
    reverseoutoftrouble:
    GOSUB triggers
    WHILE rangefront < stopreversing
    HPWM 1,Right,50
    HPWM 2,Back,50
    GOSUB triggers
    WEND

    IF rangeright > outertrack OR rangeright < desiredtrack THEN
    HPWM 1,Left,50
    steeringleft = 1
    HPWM 2,HalfForward,50
    ENDIF
    GOTO main
    '--------------------------

    triggers: ' Check three sonars and channel 3 signal.
    oldrangeright = rangeright
    ' produce 10uS trigger pulse (must be minimum of 10uS)
    LOW trigright
    HIGH trigright
    HIGH trigright
    HIGH trigright
    LOW trigright
    'zero could be legal below
    PULSIN echoright,1,rangeright 'measures the range in 10uS steps
    PAUSE 10 'Wait for ringing to stop - read SF05 spec.

    pulsf:
    oldrangefront = rangefront
    LOW trigfront
    HIGH trigfront
    HIGH trigfront
    HIGH trigfront
    LOW trigfront
    PULSIN echofront,1,rangefront ' measures the range in 10uS steps
    PAUSE 10

    Checkpot:
    ' I think Potread goes from 0 to 6.
    ADCIN PORTA.0, Potread
    Potread = Potread/37
    Forward = 105+Potread
    HalfForward = 105+(Potread/2) 'seems to work.
    Back = 95-Potread
    HalfBack = 95-(Potread/2)
    '----------
    frontdanger = 210 + Potread*55 '210-540, 14-36 inches
    stopreversing = 180 + Potread*25 '180-330, 12-22 inches
    frontfree = 700 + Potread*60 '700-1060, 48-70 inches
    desiredtrack = 180 + Potread*20 '180-300, 12-20 inches
    outertrack = 360 + Potread*15 '360-450, 24-30 inches
    '-----Below worked for Potread = 0and2. Not for 5
    'frontdanger = 210 + Potread*50 '210-510, 14-34 inches
    'stopreversing = 180 + Potread*25 'Fix backup confusion
    'frontfree = 700 + Potread*50 '700-1000, 48-72 inches
    'desiredtrack = 180 + Potread*12 '180-252, 12-16 inches
    'outertrack = 360 + Potread*12 '360-432, 24-28 inches
    '----------
    'record for posterity
    WRITE 2, WORD Potread
    WRITE 4, WORD frontdanger
    WRITE 6, WORD stopreversing
    WRITE 8, WORD frontfree
    WRITE 10, WORD desiredtrack
    WRITE 12, WORD outertrack


    '***********************
    checkchannel3:
    'works with four AA batteries pack.
    'Radio receiver puts pulses
    'on this line whenever the transmitter is turned ON.
    switchtoPIC = 2
    WHILE switchtoPIC >= 2
    COUNT channel3,65,switchtoPIC
    HIGH portc.0
    WEND
    LOW portc.0
    RETURN

    END

  5. #5
    Join Date
    Nov 2005
    Posts
    51


    Did you find this post helpful? Yes | No

    Default

    Thanks Ken I will have a read through your code to get to understand it

    Regards,
    Chuck

  6. #6
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Ken, there are 2 issues that have not been addressed since the beginning of this project.

    1. The less serious of the 2 is your use of the hardware PWM module. R/C devices, such as servos and ESC's, expect a control pulse (typically 1-2mS) at about 50Hz. You can probably go lower to 30Hz or higher to about 100Hz and things should still work fine. However, a PIC16 with a 20MHz clock (which I believe you are using) will not go below 1220Hz (PR2=0xff). Even with an 8MHz clock the lowest is 488Hz (244Hz with a 4MHz click) and is still probably to high compared to the desired 50Hz.

    2. To get a closed loop control system to work correctly, you need more than a bunch of if-then-else statements. Ideally you want to use a PID controller. A proportional-integral-derivative controller (PID controller) is a generic loop feedback mechanism. It measures a process variable (e.g. temperature), and checks it against a desired set point (e.g. 100 degrees Celsius); it then attempts to adjust the process variable by changing a controller output (e.g. PWM signal to an electric heater) which will bring the process variable closer to the desired set point. Even a simple P (proportional only) controller would be much better than what you currently have.

  7. #7
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default Pid??

    I have been wondering whether I have a basic mistake in my feedback loop. I am measuring distance and adjusting rate of speed and rate of turn. Maybe I should be measuring rates not scalars

    This means differentiating. I would need to measure two distances a fixed time apart. Then do some subtraction. I do that a little bit in my code, when I include "oldrangeright" in my state machine, but since there is no consistency to my sample time...... Rate of turn is even less clear.

    Is this the PID flaw you are suggesting? What is the solution? What are the technical limitations? Will a 16F887 do the job? Do I need C code including appropriate libraries? I do not know how to start.

    Your comments about my PWM are true. I found what works by trial and error. To get 50hz would I need to install an outside oscillator? Might I somehow divide down the internal one?
    Ken

  8. #8
    Join Date
    Oct 2016
    Posts
    1


    Did you find this post helpful? Yes | No

    Default Re: Here's some code

    I wanted to know if the code that was posted for the ESC circuit goes equally well for a 16F84? Or you must adjust the appropriate door? Thank you very much.

Similar Threads

  1. PBP Book
    By Bruce in forum Off Topic
    Replies: 83
    Last Post: - 4th October 2021, 12:55
  2. PBP Extensions, What are they?
    By PJALM in forum PBP Extensions
    Replies: 9
    Last Post: - 28th September 2021, 11:26
  3. Compiler differences between PBP 2.33 & 2.46
    By nikopolis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd May 2006, 19:01
  4. Newby- PBP wont compile for 18F (MPLAB)
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th December 2005, 23:30
  5. Making PBP code more modular
    By forgie in forum General
    Replies: 30
    Last Post: - 25th October 2005, 16:24

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts