Shop OBEX P1 Docs P2 Docs Learn Events
Having trouble using count() function — Parallax Forums

Having trouble using count() function

I'm trying to use the SimpleIDE count() function to count the number of transitions from an optical encoder that is connected to a wheel. I'm getting good transitions to the Prop chip pin as read by my oscilloscope.

The problem I'm having is that the program stops at the line between the two debugging print() statements for approximately 10 seconds. It is my understanding that the count() function runs as long as the second argument in microseconds (the default) so I set it to 1,000,000. I've tried running it without the PWM or pause() functions with no luck. If I reduce the second argument to 10,000 or 1,000, then the function will return after only a couple seconds, but I'm not sure about its results.

Does this function need to be started in its own cog to function properly?
pwm_start(1000);
 
 long pulsesPerSecond;
 int speed = 0;
 set_io_dt(CLKFREQ/1000000);
    
  while(1)
  {
    // Add main loop code here.
   
   high(PIN_MOTOR_DIR_RR);
    
   pwm_set( PIN_MOTOR_SPD_RR,  0, speed);
   
   print("Just before count()\n"); 
   pulsesPerSecond = count(PIN_MOTOR_ENCODER_RR, 1000000);
   print("Just after count()\n");
   
   print("Pulses per Second: %d Motor Speed: %d \n", pulsesPerSecond, (1000-speed));

   pause(1000);
   
   speed = speed + 5;

Comments

  • set_io_dt(CLKFREQ/1000000); will yield a 1 microsecond tick ONLY IF the propeller is being clocked at 80 MHz, 5 MHz crystal x 16 PLL.
    I would put the counting function into it's own cog and whenever the count function produces a new value have it update a global flag. The main program monitors the flag and if in a state representing no update then the program continues to use the current count. When the flag changes bring in the new count value, reset the flag and continue on.
  • I am using one of the HoverFlyOPEN boards, the crystal frequency is 5Mhz, and CLKFREQ is set to 80,000,000. Perhaps I'm missing some setting from SimpleIDE? This code shows the same behavior on a Prop development board.
  • In SimpleIde under project options, which board type is selected and how is it configured in the propeller-load folder?
  • I'm using the "Activity Board" option since the HoverFlyOPEN board isn't listed. Here is the activityboard.cfg file:
    # Propeller activity board configuration.
    # IDE:SDXMMC
        clkfreq: 80000000
        clkmode: XTAL1+PLL16X
        baudrate: 115200
        rxpin: 31
        txpin: 30
        cache-driver: eeprom_cache.dat
        cache-size: 8K
        cache-param1: 0
        cache-param2: 0
        eeprom-first: TRUE
        sd-driver: sd_driver.dat
        sdspi-do: 22
        sdspi-clk: 23
        sdspi-di: 24
        sdspi-cs: 25
    
  • FireNWaterFireNWater Posts: 93
    edited 2016-06-10 16:40
    This is all the documentation on the function. I am assuming (yeah, I know) that the "duration" argument sets how long the function runs? I'd like to get this function working before I start trying to "roll my own".
    long count (int pin, long duration)
    
    
    Count number of low to high transitions an external input applies to an I/O pin over a certain period of time. 
    Parameters
    pin
    I/O pin number 
    duration
    Amount of time the measurement counts transitions
    Returns
    The number of low to high transitions
    
  • Is there an led you can hook up to an unused pin? Turn it on just before the "pulsesPerSecond = count(PIN_MOTOR_ENCODER_RR, 1000000);" statement and turn it off following the statement. It should give you a visual indication how long that statement remains active.
    Can you attach a zip file of this project so that others can see if they get the same result?
  • I just looked over a program I wrote for a small robot that uses motor encoders. There I have two cogs counting the encoder pulses and the duration variable I used was 30 and the comment was to count pulses for 30 milliseconds.
    Just for grins use 1000 and if you have an led connected see if it has about a 1 second blink.
    //                        Right Motor Sensor COG Function
    void Rsensor()
    {
      #define RIGHT_SENSOR    5               // pin 5
      // SPAN Definition is GLOBAL  val = 30
      
      uint32_t right;
      
      while(1)
      {
        right = 0;                            // zero the var
          
        right += count(RIGHT_SENSOR, SPAN);   // count RIGHT pulses for 30 ms
        rightb = right;                       // double buffer
      }                                       // ends while(1)
    }                                         // ends this block      
    
  • FireNWaterFireNWater Posts: 93
    edited 2016-06-10 19:29
    Hal Albach wrote: »
    . . .[snip]. . .
    Just for grins use 1000 and if you have an led connected see if it has about a 1 second blink.. . .[snip]. . .
    .
    Booyah! Setting the span to 1000 did the trick. I think the documentation needs to be updated from micro-seconds to milli-seconds. . .
    .
    When I get time I guess I need to read thru the source code for this function.
    .

  • Here's the project folder. There's not much in there except trying to get this function to work.
  • Great to hear things are working out. (Thank ___ for old projects, eh?)
  • Thanks for the help Hal. I was trusting that the documentation was correct . . ugh.
  • You're welcome!
  • I'm surprised that the HoverFlyOPEN boards aren't flying off the shelf. If you ignore the accelerometer, there are tons of other peripherals already installed for bot building.
Sign In or Register to comment.