code for compass output in degrees from MPU-9250

Place code snippets and demo code here
peridot
Posts: 46
Joined: Mon Mar 08, 2021 4:54 am
Has thanked: 7 times
Been thanked: 93 times

Re: code for compass output in degrees from MPU-9250

Post by peridot »

"Oh Boy" I am really sorry I have given you all a bum steer, I actually used an AS5600 not the QMV6310. It was the AS5600 that has a analog output and yes it works perfectly straight out the box.
https://www.ebay.com.au/itm/25565134291 ... EQAvD_BwE
https://ams.com/en/as5600

My Annex weather station was posted here [Local Link Removed for Guests]
peridot
Posts: 46
Joined: Mon Mar 08, 2021 4:54 am
Has thanked: 7 times
Been thanked: 93 times

Re: code for compass output in degrees from MPU-9250

Post by peridot »

My wind dir sensor is based on this project https://www.printables.com/model/61764- ... -and-anemo, I only used the wind direction. I believe the magnet is 1-2 mm away from the sensor, the magnet , the magnet must be diametrically magnetised !
peridot
Posts: 46
Joined: Mon Mar 08, 2021 4:54 am
Has thanked: 7 times
Been thanked: 93 times

Re: code for compass output in degrees from MPU-9250

Post by peridot »

I would have thought a reed switch or Hall effect sensor would have been better for the Wind speed.

If anyone is doing weather sensors here is my code for Anemometers and wind direction.
the whole Annex32 code is attached and the subs are below.
Nylex-Sta-Annex11.zip

Code: [Local Link Removed for Guests]

Core Subs:
'Wind speed sensor interrupt
anemometerClick:
if pin(32)=0 or wproc=1 then return'only count low going pulses
thistime=millis-anem_last  'thistime measures the time between pulses
'thistime=thistime/3   '****similation for 3 pulses only - Rem when not used ****
anem_last=millis
If thistime>10 Then                     'only process if > 5mSec, for switch debounce
  anem_count=anem_count+1              'Adds up pulses single pulse per/rotation
  'anem_count=anem_count+3              'Adds up pulses  '****similation for 3 pulses only - Rem when not used ****
  If thistime<anem_min Then            'If the time is less than 1 second
    anem_min=thistime 'gust            'then thistime = gust
  EndIf
  'wlog "Wind click"               'Prints this EVERY time the reed switch close s=**TEST USE ONLY - DELETE WHEN happy its working properly
  'use this to check how many switch closures per revolution when turning by hand
EndIf
'wlog "Incr Anem counts:";anem_count;" This Pulse interval:";thistime;" Min Pulse interval:";anem_min
return

'Wind Speed data process
Sub WindSpeedProc
  wproc=1
  Local wResult, reading,x
  wlog "Repeat-time:";millis-testtime:testtime=millis
  if anem_count >0 then
    anem_api=int(testpause/anem_count)  'calc avg pulse interval
  else
    anem_api=testpause/1
  endif
  wlog "Total Anem counts:";anem_count;" Avg Pulse interval:";anem_api;" Min Pulse interval:";anem_min 'for testing purposes
  'calc wind speed over 3sec period
  wspeed=0
  If anem_count>0 Then
    wSpeed=(windfactor*anem_count)/(testpause/1000)
    'calc Gust
    wGust=(1/(anem_min/1000))*windfactor
    wlog "CalcGust ";wGust
  Else
    wspeed=0
    wgust=0
  EndIf
  'calc wind rolling average speed
  'GoTo skip   'comment out this line for a  Rolling Average
  average wSpeed, wArray(), 20, 0
  
  
  skip:
  if wGust<wspeed then wgust=wspeed
  If wgust>hgust Then hgust=wgust
  anem_count=0
  anem_min=anem_nowind
  wproc=0
End Sub

'calculate average, avgResult=var,avg()=array,el=# of elements,m=0(calculate)
sub average(avgResult,avg(),el,m)
  local x
  'wlog "Average Sub"
  'wlog avgResult
  'wlog el
  if m=1 then
    For x=1 To el      'preload array
      avg(x)=avgResult
    Next x
    exit sub
  end if
  For x=1 To el-1      'move data up array
    avg(x)=avg(x+1)
    'wlog "Show Average Array";x;":";avg(x)
  Next x
  avg(el)=avgResult    'place latest reading in Array
  For x=1 To el        'addup all data in array
    avgResult=avgResult+avg(x)
  Next x
  avgResult=avgResult/el           'get average from the array
  'wlog "AvgResult:";avgResult
end sub




sub getWinDir (wadc)
  wadc=cint(wadc)
  wlog wadc
  select case wadc
    case 981 to 1013
      wdeg=0:wdir$="N"
    case 0 to 32
      wdeg=0:wdir$="N"
    case 33 to 96
      wdeg=23:wdir$="NNE"
    case 97 to 160
      wdeg=45:wdir$="NE"
    case 161 to 224
      wdeg=68:wdir$="ENE"
    case 225 to 288
      wdeg=90:wdir$="E"
    case 289 to 352
      wdeg=113:wdir$="ESE"
    case  353 to 416
      wdeg=135:wdir$="SE"
    case 417 to 480
      wdeg=158:wdir$="SSE"
    case 481 to 544
      wdeg=180:wdir$="S"
    case 545 to 608
      wdeg=203:wdir$="SSW"
    case 609 to 672
      wdeg=225:wdir$="SW"
    case 673 to 736
      wdeg=248:wdir$="WSW"
    case 737 to 800
      wdeg=270:wdir$="W"
    case 801 to 864
      wdeg=293:wdir$="WNW"
    case 865 to 928
      wdeg=315:wdir$="NW"
    case 929 to 980
      wdeg=338:wdir$="NNW"
    case else
      wdeg=0:wdir$="CALM"
  end select
end sub
You do not have the required permissions to view the files attached to this post.
User avatar
Electroguard
Posts: 857
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 273 times
Been thanked: 321 times

Re: code for compass output in degrees from MPU-9250

Post by Electroguard »

Thank you for your code Mr P, it should be very useful. And yes, hall or opto pulse sensor might be good idea for wind speed ... maybe use that little toy wind generator in conjunction with a solar panel to charge the lipo that powers the outside stuff (often don't get enough winter sun for effective solar).
Zim
Posts: 286
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 262 times
Been thanked: 128 times

Re: code for compass output in degrees from MPU-9250

Post by Zim »

Sorry to report that GY-273 is not sensitive enough to be used as a compass, out of the box. :(
Post Reply