• AVR Freaks

Hot!Problem to solve with servos

Page: 123 > Showing page 1 of 3
Author
Adramateus
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2020/09/08 17:23:24
  • Location: 0
  • Status: offline
2020/09/09 12:48:25 (permalink)
0

Problem to solve with servos

Hi, new here and, obviously need a hand to solve my problem, since it seem I can't get my hands on the proper informations to actualy know where to go from it ...
Anyway here is that problem of mine.

Using a PIC18F25K83 with a 20Mhz external cristal. Using MPLABX IDE 5.40, XC8 2.20 and MCC.
Since I'm using servos, I need to generate PWMs and, to do so, need to go with the tmr2 that actualy will give the proper impulse of 50 Hz (As I could do) and generate a wave form of a 20ms period (also "seem" to be done too).
 
I've tried to lightly modify the code in the PWM5.c to make it more clear on what are the values needed from my main.c to give the proper DC. The only actual result is the servos getting in middle position (90° ... a clear 1.5ms/20ms output). At first it was to help me mount the structure I use for the projet (a spider) to it and not having to reajust all of it later. But now... I can't seem to actualy change the output value at all.

So here are the .c files I think would help anyone to be able ... to help me on that.
post edited by Adramateus - 2020/09/10 10:52:22
#1

51 Replies Related Threads

    PStechPaul
    Super Member
    • Total Posts : 2916
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/10 12:30:11 (permalink)
    0
    It's difficult to go through so many lines of code to even attempt to find out what's wrong. Have you used a debugger to determine if the PWM values are being set properly? Try building a simple test project that sets two different values at 1 second intervals in a loop.

     
    #2
    Adramateus
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/09/08 17:23:24
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/10 12:45:50 (permalink)
    0
    I did and was one of the first steps I did before coming here.
    Started a new project on MPLAB to make sure there is nothing else around it and checked from here.

    My main issue is that that same code already did worked in my series of tests before actualy starting to build up the structure. I wanted to make sure the pulses were proper ... that's why I'm checking here if anyone can find anything that could actualy be the main reason to this problem..

    There is probably a misconception of what the PWM5.c is supposed to receive to make the impulse toward the servo to work ... and since I cant find a clear information on what procedures to do to make it work...
    #3
    PStechPaul
    Super Member
    • Total Posts : 2916
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/10 13:33:20 (permalink)
    0
    I don't have experience with servos, so maybe someone else can help.

     
    #4
    dan1138
    Super Member
    • Total Posts : 3835
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/10 15:49:26 (permalink)
    +1 (1)
    Adramateus (aka Jonathan),

    After looking at the code you posted you appear to be creating position controls for a single limb of a hexapod.

    Each limb seems to be driven by three hobby type proportional servo motors that rotate ±90° maximum.

    The position set pulse is repeated every 20 milliseconds, a pulse width of 500 microseconds sets the position at -90°, a pulse width of 1500 microseconds sets the position at 0°, a pulse width of 2500 microseconds sets the position at +90°.

    You are facing several problems with your firmware and hardware design choices.

    The way the PWM function blocks in the PIC18F25K83 work is not well explained in the documentation and the MPLAB Code Configurator tool produces a very primitive implementation.

    What you need to do is write a clear, elementary description of how the position set plus widths sent to the servo motors need to change over time in 20 millisecond intervals. This is a process like that used by stop motion animators to make clay figures appear to move in film. You task is to describe in numbers what an animator does by moving the limbs of a clay figure by hand. This is not an easy thing to do well but with this description the code you need to implement should be more obvious.

    The trap for new players is that when the PIC18F25K83 PWM is setup for a 20 millisecond period the resolution of pulse widths between 0 to 2.5 milliseconds is not fine enough to control a hobby servo motors reliably.

    There are ways to get the PIC18F25K83 to produce high resolution pulse widths but these methods are difficult to comprehend and implement.

    As you seem to be involved in a some kind of school project I suspect you may be under intense time pressure. Please let us know when you need to complete your project.
    #5
    typematrix
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2019/03/04 08:11:26
    • Location: Ireland
    • Status: online
    Re: Problem to solve with servos 2020/09/10 17:46:15 (permalink)
    0
     
     
    Hi 
     
    Can you show us screenshots of the MCC config for PWM5  and TMR2
     
    post edited by typematrix - 2020/09/10 17:57:26
    #6
    Antipodean
    Super Member
    • Total Posts : 1904
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 04:31:29 (permalink)
    +1 (1)
    dan1138
    The trap for new players is that when the PIC18F25K83 PWM is setup for a 20 millisecond period the resolution of pulse widths between 0 to 2.5 milliseconds is not fine enough to control a hobby servo motors reliably.

     
    The period doesn't need to be 20mS, it could be shortened. I would try 10ms for a start, as that would potentially double the servo resolution, and possibly come down a bit more, to about 8mSs or whatever value gives the next bit of resolution (without working through his code to see what divisors he has).
    I don't know off hand what the shortest period is for the pulse repeat, it may vary by servo manufacturer, but it could be worth trying 5ms.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #7
    dan1138
    Super Member
    • Total Posts : 3835
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 09:57:50 (permalink)
    +1 (1)
    Antïpodean
    dan1138
    The trap for new players is that when the PIC18F25K83 PWM is setup for a 20 millisecond period the resolution of pulse widths between 0 to 2.5 milliseconds is not fine enough to control a hobby servo motors reliably.

     
    The period doesn't need to be 20mS, it could be shortened. I would try 10ms for a start, as that would potentially double the servo resolution, and possibly come down a bit more, to about 8mSs or whatever value gives the next bit of resolution (without working through his code to see what divisors he has).
    I don't know off hand what the shortest period is for the pulse repeat, it may vary by servo manufacturer, but it could be worth trying 5ms.

    The OP stated in post #1 they are "Using a PIC18F25K83 with a 20Mhz external crystal."

    The code in file tmr2.c set PR2 = 0xc2; // 194, and the prescale to 128.

    According to the data sheet the PWM frequency would be: 20000000 / 4 / 128 / 195 = 200.32 Hz, or 4.992 milliseconds

    This pulse repetition rate is too fast for an analog hobby servo. I do not know what it would do to a digital servo.

    Code from file pwm5.c:
    void PWM5_LoadDutyValue(float DutyValue)//
    {
     //calcul du dutyvalue a partir de l'angle choisie dans le main//
     //calculation of the dutyvalue from the angle chosen in the main//
     float X=(((DutyValue/180)+1)/20)*1023;
     uint16_t dutyValue=X;
     
     // Writing to 8 MSBs of PWM duty cycle in PWMDCH register
     PWM5DCH = (dutyValue & 0x03FC)>>2;
     
     // Writing to 2 LSBs of PWM duty cycle in PWMDCL register
     PWM5DCL = (dutyValue & 0x0003)<<6;
    }

    The math used to convert the angle to a PWM pulse width does not seem correct.
    post edited by dan1138 - 2020/09/11 14:42:13
    #8
    davea
    Super Member
    • Total Posts : 305
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 10:05:07 (permalink)
    0
    a different way would use CCP in compare mode and a state machine
    to start a pulse and set the next timeout to end a pulse
    and they could be multiple pulses in the state machane 
    accuracy would be very good if done right
     
    #9
    dan1138
    Super Member
    • Total Posts : 3835
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 10:19:22 (permalink)
    +1 (1)
    davea
    a different way would use CCP in compare mode and a state machine
    to start a pulse and set the next timeout to end a pulse
    and they could be multiple pulses in the state machane 
    accuracy would be very good if done right

    That could work but I suspect the skills of the Original Poster may not be up for the complexity of that challenge.
    #10
    Mysil
    Super Member
    • Total Posts : 3703
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 11:34:04 (permalink)
    0
    Hi,
    By selecting MFINTOSC  500 kHz clock signal for the Timer 2, instead of instruction clock,
    it is possible to achieve the intended 20 millisecond PWM period with a prescaler of 64. 
     
    While the hardware may be running with a 20 MHz crystal,
    MCC configuration have stumbled into using a default 1:4 clock divider,
    so actual system clock frequency is much lower than assumed in message #8.
     
    In general, my opinion is that using floating point calculations in this kind of application on a 8 bit microcontroller may be slow, especially with a 5 MHz system clock, 1.25 MHz instruction frequency.
    Rather than trying to change PWM handling to take floating point argument,
    I would rather suggest animation control to use integer calculations.
     
        Mysil
     
     
    post edited by Mysil - 2020/09/11 11:38:13
    #11
    Adramateus
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/09/08 17:23:24
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 12:08:14 (permalink)
    0
    Thanks all for your answers!

    Mysil
    The system don't allow me to make that selection actualy. It ask me to absolutely select the FOSC/4 for the use of TMR2 and PWMs. Other wise it will not generate any PWMs... and actively show me the warning in regard of that.

    davea
    For the part of the state machine. I don't even know where to start to put that up.
    I actualy use some of those in my main to check the entry variable from my controls ... but it goes only there mostly.

    dan1138
    It may be wrong ... but this is not giving me many hint on what to do. (it sound rude... and I dont want that sorry in advance =/ )
    I calculate what seem for me to be the more proper way to have the right value for the impulse in regard of the maximum period ...
    For what is in the register ... I can't really find the informations needed to properly comprehend what should be put there. I've read the datasheet ... but it seem that the information is not clear enough for my comprehension at all.
    I appreciate the help on that, very much. 


    Anyways... in my PWM parameters... all I can see is actualy the almost 20 ms (19.968) and my 50 Hz (50.08) from the selections I made.
    #12
    dan1138
    Super Member
    • Total Posts : 3835
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 14:53:53 (permalink)
    +2 (2)
    Adramateus
    dan1138
    It may be wrong ... but this is not giving me many hint on what to do. (it sound rude... and I dont want that sorry in advance =/ )



    I must apologize, my statements that the writes to update the duty cycle control register of the of PWM5 are not correct. The code generated by the MCC does do the proper thing. It is I that needs to read the data sheet with more care.
     
    At present I do not have access to real hardware with a PIC18F25K83 controller and I am trying to debug this with the MPLABX v5.40 simulator. It is not going well. This simulation tool does an awful job with this controller.
     
    I have an idea that may help but after embarrassing myself with overconfidence I will need validate what I have in the real controller before I post another suggestion.
    post edited by dan1138 - 2020/09/11 17:41:20
    #13
    PStechPaul
    Super Member
    • Total Posts : 2916
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 17:28:05 (permalink)
    0
    MPLABX 5.40 simulator is borked, at least for some functionality. I think 5.35 is OK, and George said it would be fixed in 5.45. I reverted to 5.10, but I'm just using a PIC16F1825, which is pretty simple and mature.

     
    #14
    Mysil
    Super Member
    • Total Posts : 3703
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Problem to solve with servos 2020/09/11 20:59:41 (permalink)
    +1 (1)
    Hi,
    Referring to messages #11 and #12  about using a slower clock signal for the PWM Timer,
    I have tried MFINTOSC 500 kHz for TIMER2 and PWM5 on PIC18F46K42, 
    which I believe have the same Timer and PWM peripherals.
     
    I have seen the Warnings in Microchip Code Configurator (MCC),
    and the PWM configuration menu is not able to show the PWM period and duty cycle.
    It is my opinion that these warnings are bugs/limitations in the MCC GUI,
    and that PIC hardware function as it is intended and explained in the Datasheet.
     
    With 500 kHz clock and prescaler 64, there are about 624 intervals in 20 milliseconds
    with PWM steps about 0.032 milliseconds,
    which will give about 30 resolution steps between 1.0 and 2.0 mllisecond, 
    or about 60 resolution steps between 0.5 and 2.5 millisecond.
    This is not great, but may possibly be made to work.
     
    In PIC18F__K83 and PIC18F__K42  there are several possibilities for clocking the PWM timers,
    including cascading Timers, or using the NCO frequency synthesis divider to create input clock for Timer2.
    This may make it possible to achieve a tailored input clock to Timer2.
    Note, that there still have to be a Timer2 Prescaler setting of at least 4, to achieve 10 bit PWM.
     
    Using the CCP peripherals in Output Compare mode, as suggested in other messages may give 16 bit resolution.
    There have been example code published in this forum before.
     
        Mysil
     
    #15
    davea
    Super Member
    • Total Posts : 305
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/12 13:08:20 (permalink)
    0
    assumptions made
    servos do not need updating to hold there position or at least for 100mS
    PORTA is 8 servo outouts
    1ms = 10000 needs to be scaled to TIMER1 for best resolution
    only 2 states are needed so you could remove 60% of the code
     
    in MAIN
    insure BANK2bits.Start_pulse = 0
    set channel_sel
    set Pulse_val
    set BANK2bits.Start_pulse = 1
     
    8 servos can be moved in 21mS, you wont see the lag 

    //--------------------------------------------------------------
    // pseudo case switch (4 states)
    //-------------------------------------------------------------- 
    void CCP2_CompareISR(void){ 
       uint16_t temp;
       uint8_t CCPr2L = CCPR2L; // read order, low byte then hi byte
       uint8_t CCPr2H = CCPR2H;
       //-------------------------
       temp = (CCPr2H << 8) | CCPr2L; // time now
       //-------------------------
       if ((BANK2bits.CMP_SW_bit0 == 0) && (BANK2bits.CMP_SW_bit1 == 0)) {
          // CASE 0
          if (BANK2bits.Start_pulse) {
             LATA =channel_sel; // set pin hi
             offset = Pulse_val; // 
             BANK2bits.CMP_SW_bit0 = 1; // next state
          } else {
             offset = 10000; // 1ms next check
          }
       } else {
          if ((BANK2bits.CMP_SW_bit0 == 0) && (BANK2bits.CMP_SW_bit1 == 1)) {
             // CASE 1 
             LATA = (LATA & ~chanel_sel); // set pin lo OR = 0
             BANK2bits.Start_pulse = 0; // clear it for MAIN
             offset = 10000; // 1ms next check 

             //--------------------- end for 2 states

             BANK2bits.CMP_SW_bit0 = 0; // next state
             BANK2bits.CMP_SW_bit1 = 1; // 0 for 2 states !!!
          } else {
             if ((BANK2bits.CMP_SW_bit0 == 1) && (BANK2bits.CMP_SW_bit1 == 0)) {
                // CASE 2
                BANK2bits.CMP_SW_bit1 = 1; // next state
             } else {
                // CASE 3 must be 
                BANK2bits.CMP_SW_bit0 = 0; // state 0
                BANK2bits.CMP_SW_bit1 = 0;
             }
          }
       }
       temp += offset; // 
       CCPR2H = temp >> 8; // set next ISR
       CCPR2L = temp;
    }

    #16
    Adramateus
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/09/08 17:23:24
    • Location: 0
    • Status: offline
    Re: Problem to solve with servos 2020/09/12 21:21:54 (permalink)
    0
    davea
    What I understand here is that I won't use the PWMs accessibles in the PIC, but instead would use another path to actualy generate impulses to the servos...?

    But, my other problem I could see with that is that I already got all the circuit board printed and mounted. (not on a bread board at all)
    It's just the program I need to put in it using the actual output pins I'm using for the servos for each limbs.
    One limb = one circuit plate = three servos => C4 (pin 15), C5 (pin 16) and C6 (pin 17).
    I'm prety sure I could redirect this code to target those pins anyway ... not sure to see the proper way the values in the main would affect this one of yours.

    _____________
    Mysil
    I'm not sure it would work the same on the 25K83 I'm using tho.
    Probably the one you used got some circuitry that allows this possibility.

    On the other hand.. I think I can already see the values needed. The PWM period is already clear when I look into the register : 20 ms (with 50 Hz).
    The only one not really clear is actualy the Duty Cycle... since I can set it already to a value between 0 and 100%.
    I'm not really sure if I should put it "off" to make sure only the values from the main are maybe used ... or set it to another value that would also take in consideration those in the main.
    I've put it at 50% and 100% to try... and I got the sames results (90° -> "center") without any possibilities to move out of this state in the "presetting" position (the one I've put in the beginning to, at least, have a position for the servos to start at before actualy using the states for the movements).
    I'll try to just put 0% and see the result... since it seem I've lack the brillant idea to try this one on ... >> (I'll let you all know if it work)

    For the CCP output compare mode... I'm not quite sure to understand. Does it need the use of another pin? (if it's the case, I cant ... for the same reason I says to davea).
    #17
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem to solve with servos 2020/09/12 21:53:12 (permalink)
    0
    Dummy post to try to stop the flashing.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #18
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem to solve with servos 2020/09/12 21:58:51 (permalink)
    0
    Adramateus
    davea
    What I understand here is that I won't use the PWMs accessibles in the PIC, but instead would use another path to actualy generate impulses to the servos...?

    Yes.
    The PWM peripheral is NOT well suited to generate the pulse you need, so he is suggesting an alternate procedure.

    The only one not really clear is actualy the Duty Cycle... since I can set it already to a value between 0 and 100%.
    I'm not really sure if I should put it "off" to make sure only the values from the main are maybe used ... or set it to another value that would also take in consideration those in the main.
    I've put it at 50% and 100% to try... and I got the sames results (90° -> "center") without any possibilities to move out of this state in the "presetting" position (the one I've put in the beginning to, at least, have a position for the servos to start at before actualy using the states for the movements).
    I'll try to just put 0% and see the result... since it seem I've lack the brillant idea to try this one on ... >> (I'll let you all know if it work)

    You seem to be missing the point that 50% and 100% refer to the entire 20ms cycle, whereas for your servos you need  a pulse that varies from 0.5ms to 2.5ms, so only from 2.5% to 12.5%, so you only have a tenth of the full resolution available.
    This was all explained by Dan in post#5.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #19
    PStechPaul
    Super Member
    • Total Posts : 2916
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Problem to solve with servos 2020/09/12 22:02:41 (permalink)
    0
    All right - who's the one that mentioned the "Eye-Fone"? It is forbidden!

     
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5