Shop OBEX P1 Docs P2 Docs Learn Events
Possible Aliasing — Parallax Forums

Possible Aliasing

Hello, I'm encountering weird sensor reading (pwm reading) problem and can't find the cause of this error. If anyone can share some thoughts, it would be very helpful. When I run the code below, it just works well. However, when I try to incorporate this with the main project, the pwm reading becomes irregular; say if the correct reading is 500, irregular numbers are 300-ish, 410-ish, 500 - they are quite discrete! The main project's (one of) cog calls pulse_in method to update this distance sensor reading. So far, no other codes are in the loop but "dist := pulse_in(pin)" . I first accused of a power source because a mpu is connected to the same 5V out. Yet, it's found okay. And of course, I checked if the "dist" variable is not aliased. So, my final accusation is that phsa register can be somehow corrupted? If that's not possible, what else could I accuse of? I didn't and couldn't attach the main project's code because not only it's lots of files to check and the specific cog calling this pulse_in() has one loop that calls the method and I don't think other cogs can affect this cog - stacks for the each cog are distinct!


CON
  _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
  _xinfreq = 5_000_000

VAR
  long  stack[128], ping, gctra, counter, dist
   
OBJ
  uart : "FullDuplexSerial.spin"
  
PUB main

  uart.quickStart
                       
  cognew(report, @stack) 

  repeat
    ping := pulse_in(8)  ' ping in mm
  

pub pulse_in(pin) | mask, milimeter

  mask := 1 << pin                                              ' mask for pin

  frqa := 1                                                     ' set for high-resolution measurement

  ctra := (%01000 << 26) | pin                                  ' set ctra to POS detect on pin   
  waitpne(mask, mask, 0)                                        ' wait for pin to be low
  phsa := 0                                                     ' clear accumulator
  waitpeq(mask, mask, 0)                                        ' wait for pin to go high
  waitpne(mask, mask, 0)                                        ' wait for pin to return low

  milimeter := phsa / (clkfreq / 1_000_000)   ' convert ticks to us
  
  return  milimeter                            


  
PUB report | temp

  repeat
    uart.clear
    uart.dec(ping)
    uart.newline
    temp := ping
    dist := temp'dist*70/100 + temp*30/100
    uart.dec(dist/10)
    uart.str(string("."))
    uart.dec(dist//10)
    uart.strln(string(" cm"))
    waitcnt(cnt + clkfreq/10)


Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2016-02-17 02:28
    I don't see anywhere that the distance sensor is being triggered. Are you actually using a Parallax Ping))), or a sensor that continuously triggers itself and outputs pulses?

    If the latter, you need to check for edges on both the beginning of the count and at the end. You're currently only checking for a low on the input to begin the count, which could occur any amount of time after a falling edge. I suspect the real issue is that your phsa := 0 and waitpeq(mask, mask, 0) need to be swapped.

    -Phil
  • Hm.. that doesn't correct. I might need to try with different circuit or board..
  • What kind of sensor are you using (brand and model)? Link to specs?

    -Phil
  • Yeah, it's here:

    http://www.maxbotix.com/Ultrasonic_Sensors/MB1003.htm


    Still the odd thing is, the code only for reading pwm of this sensor works fine. Is it possible that full mpu9150 reading and 50Hz of calculation codes drops power flowing in for the sensor since my project code is quite full of all memory and cogs.
  • Actually, I also tried with the ping sensor. But it also not working well when it is incorporated with the whole project while it works just well individually. I'm not sure what is going on. Except these distance sensors, the project works well; the projects output pwm, i2c and usb readings... all fine..
  • Probably, I think I'm getting a clue. How can I determin the max updating rate for pwm reading? Max pulse width?
  • What is the max distance you want to measure or the max distance the device can measure. Use that to determine what the timeout should be. See the pasm obj for ping in obex.
    Jim
Sign In or Register to comment.