Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa digunakan untuk mengendalikan sudut servo, aplikasi dari alat ini sangat banyak sekali diantara yaitu untuk mengendalikan hover pesawat, sebagai joystick robot, kendali arah belokan dan sebagainya, Alat ini menggunakan sensor compass HMC5883L atau nama lainnya GY-273, sensor ini memberikan output 0 derajat sampai 360 derajat, sehingga cocok dengan gerakan servo yang ada di range 10 - 220, maka dari itu kedua device ini sangat bagus sekali jika dikombinasikan, untuk lebih jelasnya berikut skema dan programnya.
a. Minimum System
b. Sensor GY-273
c. Motor Servo
d. Program Bascom AVR
$regfile = "m16def.dat"
$crystal = 8000000
$hwstack = 64
$swstack = 64
$framesize = 64
'-------------------------------------------------------------------------------
Config Servos = 1 , Servo1 = Portb.0 , Reload = 10
Config Portb.0 = Output
'=====[ Global Vars & Constants ]========
Dim Hmc_x As Integer
Dim Hmc_xl As Byte At Hmc_x + 0 Overlay
Dim Hmc_xh As Byte At Hmc_x + 1 Overlay
Dim Hmc_y As Integer
Dim Hmc_yl As Byte At Hmc_y + 0 Overlay
Dim Hmc_yh As Byte At Hmc_y + 1 Overlay
Dim Hmc_z As Integer
Dim Hmc_zl As Byte At Hmc_z + 0 Overlay
Dim Hmc_zh As Byte At Hmc_z + 1 Overlay
Dim Hmc_status As Byte
Dim X As Single
Dim Y As Single
Dim Z As Single
Dim Angle As Single
Dim Angleup As Single
Dim Angledown As Single
Dim A As Integer
Const Hmc_w = &H3C
Const Hmc_r = &H3D
'-------------------------------------------------------------------------------
'=====[ Misc. config ]==============
Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 400000
I2cinit
Ddrc.7 = 0
Set Portc.7
Gosub Hmc_initialize
'-------------------------------------------------------------------------------
Enable Interrupts
Main:
Do
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
If Angle > 200 Then
Angle = 200
Elseif Angle < 40 Then
Angle = 40
Else
A = Angle
Servo(1) = Angle
End If
If Pinc.7 = 0 Then
Servo(1) = 100
Angleup = Angle + 10
Angledown = Angle - 10
Goto Awal
End If
Waitms 100
'Servo(1) = 40
'Wait 1
'Servo(1) = 200
'Wait 1
Loop
Awal:
Do
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
If Angle > Angleup Or Angle < Angledown Then
Goto Main
End If
Loop
'=====[ Initialize HMC Compass ]========
Hmc_initialize:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H00
I2cwbyte &B0101_1000
I2cwbyte &H0010_0000
I2cwbyte &H0000_0000
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]===========
Hmc_readstatus:
' Read HMC Compass status
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H09
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_status , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]=========
Hmc_readdata:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H03
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_xh , Ack
I2crbyte Hmc_xl , Ack
I2crbyte Hmc_zh , Ack
I2crbyte Hmc_zl , Ack
I2crbyte Hmc_yh , Ack
I2crbyte Hmc_yl , Nack
I2cstop
Return
'------
$crystal = 8000000
$hwstack = 64
$swstack = 64
$framesize = 64
'-------------------------------------------------------------------------------
Config Servos = 1 , Servo1 = Portb.0 , Reload = 10
Config Portb.0 = Output
'=====[ Global Vars & Constants ]========
Dim Hmc_x As Integer
Dim Hmc_xl As Byte At Hmc_x + 0 Overlay
Dim Hmc_xh As Byte At Hmc_x + 1 Overlay
Dim Hmc_y As Integer
Dim Hmc_yl As Byte At Hmc_y + 0 Overlay
Dim Hmc_yh As Byte At Hmc_y + 1 Overlay
Dim Hmc_z As Integer
Dim Hmc_zl As Byte At Hmc_z + 0 Overlay
Dim Hmc_zh As Byte At Hmc_z + 1 Overlay
Dim Hmc_status As Byte
Dim X As Single
Dim Y As Single
Dim Z As Single
Dim Angle As Single
Dim Angleup As Single
Dim Angledown As Single
Dim A As Integer
Const Hmc_w = &H3C
Const Hmc_r = &H3D
'-------------------------------------------------------------------------------
'=====[ Misc. config ]==============
Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 400000
I2cinit
Ddrc.7 = 0
Set Portc.7
Gosub Hmc_initialize
'-------------------------------------------------------------------------------
Enable Interrupts
Main:
Do
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
If Angle > 200 Then
Angle = 200
Elseif Angle < 40 Then
Angle = 40
Else
A = Angle
Servo(1) = Angle
End If
If Pinc.7 = 0 Then
Servo(1) = 100
Angleup = Angle + 10
Angledown = Angle - 10
Goto Awal
End If
Waitms 100
'Servo(1) = 40
'Wait 1
'Servo(1) = 200
'Wait 1
Loop
Awal:
Do
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
If Angle > Angleup Or Angle < Angledown Then
Goto Main
End If
Loop
'=====[ Initialize HMC Compass ]========
Hmc_initialize:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H00
I2cwbyte &B0101_1000
I2cwbyte &H0010_0000
I2cwbyte &H0000_0000
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]===========
Hmc_readstatus:
' Read HMC Compass status
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H09
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_status , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]=========
Hmc_readdata:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H03
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_xh , Ack
I2crbyte Hmc_xl , Ack
I2crbyte Hmc_zh , Ack
I2crbyte Hmc_zl , Ack
I2crbyte Hmc_yh , Ack
I2crbyte Hmc_yl , Nack
I2cstop
Return
'------
e. VIDEO HASILNYA
No comments:
Post a Comment