How to monitor two rotary quadrature encoders at once?
RussM
Posts: 27
Hi everyone. For my school's senior project I need to monitor two rotary quadrature encoders simultaneously. These two encoders will be used in conjunction to measure bicycle sprockets for quality control. I have one encoder that measures angle which determines if the user has spun the bicycle sprocket while the other encoder measures the size of the teeth relative to a fixed point on a base. My code works fine when I am monitoring one encoder but as soon as I try to start monitoring a second encoder my program stops functioning properly.
I found that when I do the two "quad.start" function calls, the second encoder (in this case the size encoder) is the one that gets monitored and I am unsure why. I tried allocating each call of "quad.start" to a new cog but that didn't work which I'm guessing is due to the fact that the quad function creates a new cog to monitor the encoders anyway. Would anyone know how I could monitor each encoder simultaneously? Thank you for any help!
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 '_xinfreq = 6_250_000 TX_PIN = 0 BAUD = 19_200 MS_001 = CLK_FREQ / 1_000 CLK_FREQ = ((_clkmode-xtal1)>>6)*_xinfreq LCD_PIN = 27 LCD_BAUD = 19_200 LCD_LINES = 4 LCD_COLS = 20 ENCODER_PIN_SIZE = 5 INDEX_PIN_SIZE = 6 ENCODER_PIN_ANGLE = 1 INDEX_PIX_ANGLE = 2 OBJ quad : "QuadDecoder" LCD : "FullDuplexSerial" f32 : "F32" f32_orig : "Float32Full" fs : "FloatString" VAR long offset_size ' example variable that will be accumulated to long offset_angle long distance long temp long distance_tick long f1 long fA long angle long angle_tick byte cog long stack[90] PUB main LCD.start(TX_PIN, TX_PIN, 00, 19_200) 'Initialize FullDuplexSerial.spin f32.start f32_orig.start offset_size := 0 ' initialize the accumulator offset_angle := 0 ' You can set it to any desired value LCD.tx($0C) LCD.tx($11) quad.start(ENCODER_PIN_ANGLE, @offset_angle) 'start the angle encoder quad.start(ENCODER_PIN_SIZE, @offset_size) ' start the size encoder LCD.str(string("Size =")) LCD.tx($0D) LCD.str(string("Angle =")) LCD.tx($0D) distance_tick := 0.000983 distance := 0.0 angle_tick := 0.144 angle := 0.0 repeat distance := f32_orig.FMul(f32_orig.FFloat(offset_size), distance_tick) angle := f32_orig.FMul(f32_orig.FFloat(offset_angle), angle_tick) LCD.tx($87) LCD.str(fs.FloatToFormat(distance,13,6)) LCD.tx($9D) LCD.str(fs.FloatToFormat(angle,10,4)) waitcnt(clkfreq/100 + cnt)
I found that when I do the two "quad.start" function calls, the second encoder (in this case the size encoder) is the one that gets monitored and I am unsure why. I tried allocating each call of "quad.start" to a new cog but that didn't work which I'm guessing is due to the fact that the quad function creates a new cog to monitor the encoders anyway. Would anyone know how I could monitor each encoder simultaneously? Thank you for any help!
Comments
The object needs to be named differently. Theres a few ways to do this.
You can do like I did and rename them differently, this means you take up more codespace for the same code.
Then when you go to use either encoder, you address them with their names properly (quad1.start and quad2.start)
Or to save codespace you can do this.
In this situation you must use the object like this:
quad[0].start
quad[1].start
Many quadrature encoder obex objects allow more than one encoder to be connected. In that case, the pins for the encoders must be sequentially connected to the prop.
The data is found by addressing the dataspace properly, and that reminds me, make sure your variables that the encoder object uses are duplicated also, two encoder objects will need two sets of variables.