Poziomnica na Rainbowleds i MPU6050

Pytania, kody i porady dotyczące nie tylko Bascom.
ODPOWIEDZ
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Poziomnica na Rainbowleds i MPU6050

Post autor: niveasoft » 27 gru 2019, 18:06

Tak kodzik który taśmę Rainbowled`ów i żyroskop zamienia w poziomnicę.
Po małej przeróbce (bo teraz wyskalowane na +/-90stopni) można sobie przykręcić do huśtawki :D

Kiedy czujnik jest w poziomie to led na samym srodku taśmy swieci na czerwono i jest otoczony dwoma zielonymi ;)
Kiedy czujnik ma inny kąt to trzy ledy przemieszczają się po taśmie znieniając kolor na czerwony tym bardziej im większy kąt.
  1. $regfile = "m328pdef.dat"
  2. $crystal = 16000000
  3. $hwstack = 64
  4. $swstack = 16
  5. $framesize = 64
  6. $baud = 115200
  7.  
  8. Const Number_of_leds = 45
  9. Const Pwm_8bit = 32                                         '0-254
  10. Const Pwm_proc = 15                                         '0-100
  11.  
  12. Config Submode = New                                        'first write subs then use them
  13.  
  14. '*************************************************
  15. '*         I2C/TWI CONFIG FOR MPU6050            *
  16. '*************************************************
  17.  $lib "i2c_twi.lib"                                         ' AVR hardware TWI (I2C)
  18.   Config Scl = Portc.5
  19.   Config Sda = Portc.4
  20.    I2cinit
  21.   Config Twi = 400000
  22.  
  23. '*************************************************
  24. '*        TIMER2 CONFIG FOR TIME BASE            *
  25. '*************************************************
  26.  
  27.   Config Timer2 = Timer , Prescale = 1024 , Clear_timer = 1
  28.   Compare2a = 155                                           '10ms @16MHz/1024
  29.  
  30.   Dim Miliseconds As Byte
  31. '*************************************************
  32. '*                RAINBOWLEDS                    *
  33. '*************************************************
  34.  
  35.   Config Rainbow = 1 , Rb0_len = Number_of_leds , Rb0_port = Portc , Rb0_pin = 0
  36. '                                                   ^ connected to pin 0
  37. '                                       ^------------ connected to portB
  38. '                         ^-------------------------- 8 leds on stripe
  39. '              ^------------------------------------- 1 channel
  40.  
  41.  
  42. 'Global Color-variables
  43. Dim Color1(3) As Byte
  44. Cr Alias Color1(_base) : Cg Alias Color1(_base + 1) : Cb Alias Color1(_base + 2)
  45.  
  46. Dim Color2(3) As Byte
  47. Cr2 Alias Color2(_base) : Cg2 Alias Color2(_base + 1) : Cb2 Alias Color2(_base + 2)
  48.  
  49. Const Zero_base_ledow = Number_of_leds - 1
  50. '*************************************************
  51. '*                 VARIABLES                     *
  52. '*************************************************
  53.  
  54. 'MPU6050
  55.   Const Mpu6050_write = &HD0
  56.   Const Mpu6050_read = &HD1
  57.  
  58.    Dim Tempbuff(14) As Byte , Helpb As Byte , Tempb As Byte
  59.    Dim Rawbuff(14) As Byte
  60.    Dim Dx As Integer At Rawbuff(1) Overlay
  61.    Dim Dy As Integer At Rawbuff(3) Overlay
  62.    Dim Dz As Integer At Rawbuff(5) Overlay
  63.    Dim Th As Integer At Rawbuff(7) Overlay
  64.    Dim Gx As Integer At Rawbuff(9) Overlay
  65.    Dim Gy As Integer At Rawbuff(11) Overlay
  66.    Dim Gz As Integer At Rawbuff(13) Overlay
  67.  
  68.    Dim Mypos As Byte , Old_pos As Byte , Heat As Byte
  69.    Dim Myint As Integer , Tempi As Integer
  70.    Dim Divider As Integer
  71.  
  72.   'digital filter for smooth move
  73.    Dim Suma As Dword , Helpd As Dword
  74.  
  75.    Dim Led_half As Byte
  76.    Led_half = Number_of_leds / 2
  77.  
  78. Sub Init_mpu
  79.  
  80. '--- (25 ) Sample Rate Divider = 1 ---
  81.    I2cstart
  82.    I2cwbyte Mpu6050_write
  83.    I2cwbyte 25
  84.    I2cwbyte &B00000000
  85.    I2cstop
  86.  
  87.    '--- (26 ) DLPF = 42/44 Hz ---
  88.    I2cstart
  89.    I2cwbyte Mpu6050_write
  90.    I2cwbyte 26
  91.    I2cwbyte &B00000011
  92.    I2cstop
  93.  
  94.    '--- (27 ) Gyro Full Range = +-2000°/s ---
  95.    I2cstart
  96.    I2cwbyte Mpu6050_write
  97.    I2cwbyte 27
  98.    I2cwbyte &B00011000
  99.    I2cstop
  100.  
  101.    '--- (28 ) ACC Full Range = +-2g ---
  102.    I2cstart
  103.    I2cwbyte Mpu6050_write
  104.    I2cwbyte 28
  105.    I2cwbyte &B00000000
  106.    I2cstop
  107.  
  108.    '--- (107) Power Management 1 ---
  109.    I2cstart
  110.    I2cwbyte Mpu6050_write
  111.    I2cwbyte 107
  112.    I2cwbyte &B00001011
  113.    I2cstop
  114.  
  115. End Sub
  116.  
  117. Call Init_mpu
  118.  
  119. Sub Read_mpu6050
  120.  
  121.    Tempbuff(1) = 59                                         'address of first byte to read
  122.    I2creceive Mpu6050_read , Tempbuff(1) , 1 , 14
  123.    Helpb = Memcopy(tempbuff(1) , Rawbuff(1) , 14)
  124.    Swap Dx : Swap Dy : Swap Dz : Swap Th : Swap Gx : Swap Gy : Swap Gz
  125.  
  126. End Sub
  127.  
  128. Waitms 50
  129. Rb_selectchannel 0                                          ' select first channel
  130. Rb_clearcolors
  131. Rb_send
  132. Cr = 75 : Cg = 25 : Cb = 0
  133.  
  134. Divider = 30000 / Number_of_leds                            'MPU605 values are trimmed in this code from -15000 to 15000 = range 30000
  135.  
  136.  
  137. Function Map(byval Value As Byte , Byval Min_in As Byte , Byval Max_in As Byte , Byval Min_out As Byte , Byval Max_out As Byte)as Byte
  138.  Local I1 As Integer
  139.  Local I2 As Integer
  140.    I1 = Value - Min_in
  141.    I2 = Max_out - Min_out
  142.    I1 = I1 * I2
  143.    I2 = Max_in - Min_in
  144.    I1 = I1 / I2
  145.    I1 = I1 + Min_out
  146.    Map = Abs(i1)
  147.    If Map >= Max_out Then Map = Max_out - 1
  148. End Function
  149.  
  150. Function Bright_8bit(byval Value As Byte , Byval 8bit As Byte)as Byte
  151.   Local W1 As Word
  152.   W1 = Value * 8bit
  153.   Shift W1 , Right , 8
  154.   Bright_8bit = W1
  155. End Function
  156.  
  157. Function Bright_procent(byval Value As Byte , Byval Procents As Byte)as Byte
  158.   Local W2 As Word
  159.   W2 = Value * Procents
  160.   W2 = W2 / 100
  161.   Bright_procent = W2
  162. End Function
  163.  
  164. Do
  165.  
  166.   If Tifr2.ocf2a = 1 Then                                   '10ms
  167.     Tifr2.ocf2a = 1
  168.  
  169.      If Miliseconds < 2 Then
  170.       Incr Miliseconds
  171.      Else
  172.       Miliseconds = 0
  173.  
  174.           Call Read_mpu6050
  175.  
  176.           Myint = Dx                                        'X axis
  177.           'Myint = Dy                                       'Y axis
  178.           'Myint = Dz                                       'Z axis
  179.  
  180.           If Myint < -15000 Then Myint = -15000
  181.           If Myint > 15000 Then Myint = 15000
  182.           Myint = Myint + 15000                             'offset for unsigned value 0-30000
  183.  
  184.          'digital filter for smoothness
  185.          '-------------------------
  186.           Helpd = Suma
  187.           Shift Helpd , Right , 3
  188.           Suma = Suma - Helpd
  189.           Suma = Suma + Myint
  190.           Helpd = Suma
  191.           Shift Helpd , Right , 3
  192.           Myint = Helpd
  193.          '-------------------------
  194.  
  195.           Tempi = Myint / Divider
  196.           Mypos = Tempi                                     '
  197.  
  198.           'Print Myint ; "  ->" ; Mypos
  199.           If Mypos > Zero_base_ledow Then Mypos = Zero_base_ledow
  200.  
  201.           If Old_pos <> Mypos Then
  202.             Old_pos = Mypos
  203.  
  204.            Select Case Mypos
  205.             Case Is < Led_half
  206.              Heat = Led_half - Mypos
  207.             Case Led_half
  208.              Heat = 0
  209.             Case Is > Led_half
  210.              Heat = Mypos - Led_half
  211.            End Select
  212.  
  213.            Rb_clearcolors                                   'always clear
  214.  
  215.            '--------------------[MAIN DOT1]------
  216.            Tempb = Map(heat , 0 , Led_half , 0 , 254)
  217.              'Print Heat ; " -> " ; Tempb
  218.            Cr = Tempb
  219.            Cg = 254 - Tempb
  220.            If Heat <> 0 Then                                'only if not centered
  221.             Rb_setcolor Mypos , Color1(_base)
  222.            End If
  223.  
  224.            '---------[DIMMING DOTS ON THE SIDES]-
  225.            Cr2 = Bright_8bit(cr , Pwm_8bit)
  226.            Cg2 = Bright_8bit(cg , Pwm_8bit)
  227.  
  228.             'Cr2 = Bright_procent(cr , Pwm_proc)
  229.             'Cg2 = Bright_procent(cg , Pwm_proc)
  230.  
  231.            '--------------------[BOTTOM DOT]------
  232.            If Mypos > 0 Then
  233.             Helpb = Mypos - 1
  234.              Rb_setcolor Helpb , Color2(_base)
  235.            End If
  236.  
  237.            '--------------------[TOP DOT]---------
  238.            If Mypos < Zero_base_ledow Then
  239.             Helpb = Mypos + 1
  240.              Rb_setcolor Helpb , Color2(_base)
  241.            End If
  242.  
  243.            '--------------------[MAIN DOT2]-------'
  244.            If Heat = 0 Then                                 'if centered then effect "red dot between greens"
  245.             Cr = 254 : Cg = 0
  246.              Rb_setcolor Mypos , Color1(_base)
  247.            End If
  248.            '--------------------------------------
  249.  
  250.  
  251.            Rb_send
  252.  
  253.           End If
  254.  
  255.  
  256.      '100ms
  257.      End If
  258.  
  259.   '10ms
  260.   End If
  261.  
  262. Loop
  263. End
ODPOWIEDZ