Having trouble using count() function
FireNWater
Posts: 93
in Robotics
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?
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
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.
Can you attach a zip file of this project so that others can see if they get the same result?
Just for grins use 1000 and if you have an led connected see if it has about a 1 second blink.
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.
.