Hallo
Nun sind die Servos angeschlossen und werden wahlweise (Umschaltung mit Z/C) mit dem analogen Stick oder den Beschleunigungssensoren gesteuert:
Bild hier
http://www.youtube.com/watch?v=gIlwGfXJxvM
Trotz minimalem Rechenaufwand kann man schon erkennen, das hier etwas funktioniert :) Allerdings noch sehr "zappelig" und ungenau. Ich vermute auch, dass mit den eingelesenen Werten etwas nicht ganz stimmt.
Das inzwischen gewachsene Programm kann nun den Nunchuk abfragen, Servos steuern, auf dem LCD ausgeben und die Tasten meines LCDs einlesen. Erstaunlich wie einfach das alles mit Bascom funktioniert:
Code:
'WII-Nunchuk mit Bascom auslesen. Grundgerüst des Codes von Linux_80 5.8.08 mic
'https://www.roboternetz.de/phpBB2/viewtopic.php?p=389309
'$regfile = "M8def.dat" ' the used chip
'$crystal = 16000000 ' frequency used
'$baud = 9600
$regfile = "M32def.dat" ' RP6 mit Mega32
$crystal = 8000000 ' taktet mit 8MHz
$baud = 38400 ' Loader-Baud
$lib "i2c_twi.lbx" ' Für Hardware TWI
'Config Scl = Portc.5 ' Ports fuer IIC-Bus
'Config Sda = Portc.4
Config Scl = Portc.0 ' Ports fuer IIC-Bus
Config Sda = Portc.1
Config Twi = 400000 ' Init TWBR und TWSR
I2cinit
' TWI gleich einschalten, das macht Bascom ansonsten erst beim I2CStart !
Twcr = &B00000100 ' nur TWEN setzen
'Const Nunchuck_write = &H52 ' Slaveadresse
'Const Nunchuck_read = &H53
Const Nunchuck_write = &HA4 ' Slaveadresse
Const Nunchuck_read = &HA5
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portb.7 , Db7 = Portb.1 , E = Portb.0 , Rs = Portc.6
Config Lcd = 20 * 4
Deflcdchar 0 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63
Cls 'clear the LCD display
Cursor Off Noblink
Lcd " RP6 ROBOT SYSTEM" 'display this at the top line
Config Servos = 2 , Servo1 = Porta.0 , Servo2 = Porta.1 , Reload = 10
Servo(1) = 90
Servo(2) = 85
Config Porta.0 = Output
Config Porta.1 = Output
Enable Interrupts
Dim A As Byte
Dim Tmp As Byte
Dim Pos_x As Byte
Dim Pos_y As Byte
Dim Acc_x As Word
Dim Acc_y As Word
Dim Acc_z As Word
Dim Buttons As Byte
Dim Tasten As Byte
Dim Stick As Byte
' Startausgabe
Print
Print "I2C-TWI Demo mit Wii Nunchuck"
Print "Ausgabe auf dem LCD beachten"
Waitms 1000
Gosub Nunchuck_init
Cls
'Lcd " WII-Nunchuk am RP6"
Lcd "acceleration values: "
Stick = 1
Do
'(
Gosub Getkeys
Locate 1 , 1
Lcd Bin(tasten)
')
Gosub Nunchuk_read
'Print Hex(pos_x) ; " " ; Hex(pos_y)
'(
Locate 2 , 1
Lcd Hex(pos_x)
Lcd " "
Lcd Hex(pos_y)
Locate 2 , 19
If Buttons.0 = 1 Then
Lcd "Z"
Else
Lcd "z"
End If
If Buttons.1 = 0 Then
Lcd "C"
Else
Lcd "c"
End If
Locate 3 , 1
Lcd "Accs X: " ; Hex(acc_x) ; " y: " ; Acc_y;
Locate 4 , 6
Lcd "Z: " ; Acc_z
')
Locate 2 , 1
Lcd "X: " ; Bin(acc_x)
Locate 3 , 1
Lcd "Y: " ; Bin(acc_y)
Locate 4 , 1
Lcd "Z: " ; Bin(acc_z)
If Buttons.0 = 1 Then Stick = 0
If Buttons.1 = 0 Then Stick = 1
If Stick = 0 Then
Servo(1) = Acc_y
Servo(2) = Acc_x
Else
Servo(1) = Pos_y - 30
Servo(2) = Pos_x - 30
End If
Waitms 100
Loop
End
Nunchuck_init:
I2cstart
I2cwbyte Nunchuck_write
I2cwbyte &H40 ' sends memory address
I2cwbyte &H00 ' sends sent a zero.
I2cstop
Return
Nunchuk_read:
Dim Buffer(6) As Byte
I2cstart
I2cwbyte Nunchuck_write
I2cwbyte &H00 ' sends one byte
I2cstop
Waitms 1
Buffer(1) = 0
I2creceive Nunchuck_read , Buffer(1) , 0 , 6
Tmp = Buffer(1) Eor &H17
'Tmp = Tmp + &H17
Pos_x = Tmp
Tmp = Buffer(2) Eor &H17
'Tmp = Tmp + &H17
Pos_y = Tmp
Tmp = Buffer(3) Eor &H17
Tmp = Tmp + &H17
Acc_x = Tmp * 4
Acc_x = Acc_x And &HFFC
Tmp = Buffer(4) Eor &H17
Tmp = Tmp + &H17
Acc_y = Tmp * 4
Acc_y = Acc_y And &HFFC
Tmp = Buffer(5) Eor &H17
Tmp = Tmp + &H17
Acc_z = Tmp * 4
Acc_z = Acc_z And &HFFC
Tmp = Buffer(6) Eor &H17
Tmp = Tmp + &H17
Buttons = Tmp
If Buttons.2 = 1 Then Set Acc_x.0 ' Die acc-LSB einlesen
If Buttons.3 = 1 Then Set Acc_x.1
If Buttons.4 = 1 Then Set Acc_y.0
If Buttons.5 = 1 Then Set Acc_y.1
If Buttons.6 = 1 Then Set Acc_z.0
If Buttons.7 = 1 Then Set Acc_z.1
Return
Getkeys:
' Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portb.7 , Db7 = Portb.1 , E = Portb.0 , Rs = Portc.6
Tasten = 0
'Disable Interrupts
Config Pinc.4 = Input
Config Pinc.5 = Input
Config Pinb.7 = Input
Config Pinb.1 = Input
Config Pinc.6 = Input
Set Portc.4
Set Portc.5
Set Portb.7
Set Portb.1
Set Portc.6
If Pinc.4 = 0 Then Set Tasten.3
If Pinc.5 = 0 Then Set Tasten.2
If Pinb.7 = 0 Then Set Tasten.1
If Pinb.1 = 0 Then Set Tasten.0
If Pinc.6 = 0 Then Set Tasten.4
Reset Portc.4
Reset Portc.5
Reset Portb.7
Reset Portb.1
Reset Portc.6
Config Pinc.4 = Output
Config Pinc.5 = Output
Config Pinb.7 = Output
Config Pinb.1 = Output
Config Pinc.6 = Output
'Enable Interrupts
Return
Beim Durchlesen ist mir aufgefallen, dass ich dem servo()-Array die 10-bit Werte der Beschleunigungssensoren zuweise. Leider finde ich in der Bascom-Hilfe keinen Hinweis wieviele Bits servo() speichert. Es gibt noch viel zu erforschen...
Gruß
mic
Lesezeichen