Perancangan Kontroler Game Mobile Menggunakan Gyroscope Dengan Algoritma Pid Dan Kalman Filtering Berbasis Arduino Nano Dan Unity
xlvii
LISTING PROGRAM
1.
Arduino
#include
#include
#include
SoftwareSerial BTserial(10, 9); // RX | TX
#define RESTRICT_PITCH
Kalman kalmanX;
Kalman kalmanY;
/* IMU Data */
double accX, accY, accZ;
double gyroX, gyroY, gyroZ;
int16_t tempRaw;
int erorr1,erorr,pidx1,pidx2,pidy1,pidy2,error,error1,lastE1,lastE,Kp,Kd,Ki;//PID
double gyroXangle, gyroYangle; // menghitung angel dengan menggunakan gyro
double compAngleX, compAngleY; // Menghitung angel dengan menggunakan complement filter
double kalAngleX, kalAngleY; // menghitung angel menggunakan Kalman filter
uint32_t timer;
uint8_t i2cData[14]; // Penampung data pada komunikasi I2C
// TODO: Make calibration routine
void setup() {
BTserial.begin(9600);
Universitas Sumatera Utara
xlviii
Serial.begin(115200);
Wire.begin();
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set frekuensi menjadi 400khz pada komunikasi I2C
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set frekuensi menjadi 400khz pada komunikasi I2C
#endif
i2cData[0] = 7; // mengambil sampel 1000Hz - 8kHz/(7+1) = 1000Hz
i2cData[1] = 0x00; // mematikan FSYNC dan mengeset 260 Hz Acc filtering, 256 Hz Gyro
filtering, 8 KHz sampel data
i2cData[2] = 0x00; // mengeset Gyro Full Scale dengan Range to ±250deg/s
i2cData[3] = 0x00; // mengeset Accelerometer Full Scale dengan Range to ±2g
while (i2cWrite(0x19, i2cData, 4, false)); // menulis empat register dengan 1 waktu
while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep
mode
while (i2cRead(0x75, i2cData, 1));
if (i2cData[0] != 0x68) {
Serial.print(F("Error reading sensor"));
while (1);
}
delay(100);
/* mengambil kalman and gyro pada angle awal */
while (i2cRead(0x3B, i2cData, 6));
accX = (int16_t)((i2cData[0] 0.1) {
velocity *= forwardSpeed;
} else if (v < -0.1) {
velocity *= backwardSpeed;
}
if (Input.GetButtonDown("Jump")) {
if (currentBaseState.nameHash == locoState){
if(!anim.IsInTransition(0))
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
anim.SetBool("Jump", true);
}
}
}
transform.localPosition += velocity * Time.fixedDeltaTime;
transform.Rotate(0, h * rotateSpeed, 0);
if (currentBaseState.nameHash == locoState){
if(useCurves){
Universitas Sumatera Utara
lxi
resetCollider();
}
}
else if(currentBaseState.nameHash == jumpState)
{
cameraObject.SendMessage("setCameraPositionJumpView");
if(!anim.IsInTransition(0))
{
if(useCurves){
float jumpHeight = anim.GetFloat("JumpHeight");
float gravityControl = anim.GetFloat("GravityControl");
if(gravityControl > 0)
rb.useGravity = false;
Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.distance > useCurvesHeight)
{
col.height = orgColHight - jumpHeight;
float adjCenterY = orgVectColCenter.y + jumpHeight;
col.center = new Vector3(0, adjCenterY, 0);
}
else{
resetCollider();
}
}
}
anim.SetBool("Jump", false);
}
}
else if (currentBaseState.nameHash == idleState)
{
if(useCurves){
resetCollider();
}
if (Input.GetButtonDown("Jump")) {
anim.SetBool("Rest", true);
}
}
else if (currentBaseState.nameHash == restState)
{
if(!anim.IsInTransition(0))
{
anim.SetBool("Rest", false);
Universitas Sumatera Utara
lxii
}
}
}
void resetCollider()
{
col.height = orgColHight;
col.center = orgVectColCenter;
}
}
4.
Untuk Pergerakan Kamera
using UnityEngine;
using System.Collections;
public class ThirdPersonCamera : MonoBehaviour
{
public float smooth = 3f;
Transform standardPos;
Transform frontPos;
Transform jumpPos;
bool bQuickSwitch = false;
void Start()
{
standardPos = GameObject.Find ("CamPos").transform;
if(GameObject.Find ("FrontPos"))
frontPos = GameObject.Find ("FrontPos").transform;
if(GameObject.Find ("JumpPos"))
jumpPos = GameObject.Find ("JumpPos").transform;
transform.position = standardPos.position;
transform.forward = standardPos.forward;
}
void FixedUpdate ()
{
if(Input.GetButton("Fire1"))
{
setCameraPositionFrontView();
}
else if(Input.GetButton("Fire2"))
{
setCameraPositionJumpView();
Universitas Sumatera Utara
lxiii
}
else if(Input.GetKey(KeyCode.Escape))
{
Application.Quit ();
}
else
{
// return the camera to standard position and direction
setCameraPositionNormalView();
}
}
void setCameraPositionNormalView()
{
if(bQuickSwitch == false){
transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.fixed
DeltaTime * smooth);
transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.fixed
DeltaTime * smooth);
}
else{
transform.position = standardPos.position;
transform.forward = standardPos.forward;
bQuickSwitch = false;
}
}
void setCameraPositionFrontView()
{
bQuickSwitch = true;
transform.position = frontPos.position;
transform.forward = frontPos.forward;
}
void setCameraPositionJumpView()
{
bQuickSwitch = false;
transform.position = Vector3.Lerp(transform.position, jumpPos.position, Time.fixedDeltaTi
me * smooth);
transform.forward = Vector3.Lerp(transform.forward, jumpPos.forward, Time.fixedDeltaTim
e * smooth);}
}
Universitas Sumatera Utara
lxiv
B-1
DAFTAR RIWAYAT HIDUP
CURRICULUM VITAE
PERSONAL DATA
Full Name
: Bobby Putra Johan
Nick Name
: Bobby
Place/ Date of Birth
: Medan/April 20rd 1994
Sex
: Male
Religion
: Islam
Nationality
: Indonesia
Address
: Perumnas Simalingkar, Kec, Pancur Batu
Mobile Phone
: 081361115937
E-mail
: bat_devil5@yahoo.com
Marital Status
: unmarried / married
EDUCATION
Bachelor of Computer Science
University of Sumatera Utara, Medan
2012-Present
Higher Secondary Education
SMK TELKOM SANDHY PUTRA
2008-2011
Universitas Sumatera Utara
lxv
Secondary Education
SMPN 2 MEDAN
2005-2008
Primary Education
SDN 068008 MANGGA
1999-2005
COMPUTER SKILL
Programming : C#, C
Database
: MySQL, Sqlite, Ms. Access
IDE
: Unity, Free Pascal, Dev C++, Arduino ,CV-Avr
Other
:-
PROJECT UNDERTAKEN
Fire fighting Robot
Line Follower Robot
Automatic Locking System
Universitas Sumatera Utara
lxvi
ORGANIZATIONAL EXPERIENCES
No
Organization
Position
Year
1
Sikonek (Sistem kontrol dan elektronika)
Anggota Humas
2013-2014
2
Sikonek (Sistem kontrol dan elektronika)
Koordinator
Litbang(penelitian
dan pengembangan)
2014-2015
3
IKLC USU
Koordinator
2014-2015
Organisasi dan
Arsitektur Komputer
SEMINARS
No. Seminar
Year
1
Si GELITIK
2013
4
SENARAI
2014
“I hereby declare that all information above is true and correct to the best of my knowledge.”
Medan,21 Juli 2017
Bobby Putra Johan
Universitas Sumatera Utara
lxvii
DAFTAR PUSTAKA
Cotta, A. & Devidas, N. T. 2016. Wireless Communication Using HC-05 Bluetooth Module
Interfaced with Arduino. International Journal of Science, Engineering and
Technology Research (IJSETR) 5(4) : 869-872.
Kurniawan, T., Hanafi, L. & Apriliani, E. 2013. Penerapan Metode Filter Kalman dalam
Perbaikan Hasil Prediksi Cuaca dengan Metode ARIMA 1-7.
Kalane, P. & Loni, P. 2012. Target Tracking Using Kalman Filter. International Journal of
Science & Technology 2(2) : 17-24 (Online) www.ijst.co.in.
Quadri, S. A. & Sidek. O. 2014. Error and Noise Analysis in an IMU using Kalman Filter.
International Journal of Hybrid Information Technology 7(3) : 39-48 (Online)
http://dx.doi.org/10/14257/ijhit.2014.7.3.06.
Fahmedha, N,. Prakash, P. C,. Pooja, A,. & Rachana, R. 2015. Estimation of System
Parameters Using Kalman Filter and Extended Kalman Filter. International
Journal of Advanced Technology and Engineering Exploration 2(6): 84-89.
Sierociuk, D. & Dzielinski, A. 2006. Farctional Kalman Filter Algorithm for The State,
Parameters and Order of Fractional System Estimation. International Journal of
Applications Math and Computer Science 16(1): 129-140.
Bassi, S.J,. Mishra, M.K,. & Omizegba, E.E. 2011. Automatic Tuning of ProportionalIntegral-Derivative (PID) Controller using Particle Swarm Optimization (PSO)
Algorithm. International Journal of Artificial Intelligence & Applications (IJAIA)
2(4) : 25-34 (Online) DOI:10.5121/ijaia.2011.2403.
Mon, Y. 2015. The Gyroscope Senssor Test by Using Arduino Platform. International
Journal of Sciencetific and Technology Research (IJSTR) 4(6): 398-400 (Online)
www.ijstr.org
Universitas Sumatera Utara
LISTING PROGRAM
1.
Arduino
#include
#include
#include
SoftwareSerial BTserial(10, 9); // RX | TX
#define RESTRICT_PITCH
Kalman kalmanX;
Kalman kalmanY;
/* IMU Data */
double accX, accY, accZ;
double gyroX, gyroY, gyroZ;
int16_t tempRaw;
int erorr1,erorr,pidx1,pidx2,pidy1,pidy2,error,error1,lastE1,lastE,Kp,Kd,Ki;//PID
double gyroXangle, gyroYangle; // menghitung angel dengan menggunakan gyro
double compAngleX, compAngleY; // Menghitung angel dengan menggunakan complement filter
double kalAngleX, kalAngleY; // menghitung angel menggunakan Kalman filter
uint32_t timer;
uint8_t i2cData[14]; // Penampung data pada komunikasi I2C
// TODO: Make calibration routine
void setup() {
BTserial.begin(9600);
Universitas Sumatera Utara
xlviii
Serial.begin(115200);
Wire.begin();
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set frekuensi menjadi 400khz pada komunikasi I2C
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set frekuensi menjadi 400khz pada komunikasi I2C
#endif
i2cData[0] = 7; // mengambil sampel 1000Hz - 8kHz/(7+1) = 1000Hz
i2cData[1] = 0x00; // mematikan FSYNC dan mengeset 260 Hz Acc filtering, 256 Hz Gyro
filtering, 8 KHz sampel data
i2cData[2] = 0x00; // mengeset Gyro Full Scale dengan Range to ±250deg/s
i2cData[3] = 0x00; // mengeset Accelerometer Full Scale dengan Range to ±2g
while (i2cWrite(0x19, i2cData, 4, false)); // menulis empat register dengan 1 waktu
while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep
mode
while (i2cRead(0x75, i2cData, 1));
if (i2cData[0] != 0x68) {
Serial.print(F("Error reading sensor"));
while (1);
}
delay(100);
/* mengambil kalman and gyro pada angle awal */
while (i2cRead(0x3B, i2cData, 6));
accX = (int16_t)((i2cData[0] 0.1) {
velocity *= forwardSpeed;
} else if (v < -0.1) {
velocity *= backwardSpeed;
}
if (Input.GetButtonDown("Jump")) {
if (currentBaseState.nameHash == locoState){
if(!anim.IsInTransition(0))
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
anim.SetBool("Jump", true);
}
}
}
transform.localPosition += velocity * Time.fixedDeltaTime;
transform.Rotate(0, h * rotateSpeed, 0);
if (currentBaseState.nameHash == locoState){
if(useCurves){
Universitas Sumatera Utara
lxi
resetCollider();
}
}
else if(currentBaseState.nameHash == jumpState)
{
cameraObject.SendMessage("setCameraPositionJumpView");
if(!anim.IsInTransition(0))
{
if(useCurves){
float jumpHeight = anim.GetFloat("JumpHeight");
float gravityControl = anim.GetFloat("GravityControl");
if(gravityControl > 0)
rb.useGravity = false;
Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.distance > useCurvesHeight)
{
col.height = orgColHight - jumpHeight;
float adjCenterY = orgVectColCenter.y + jumpHeight;
col.center = new Vector3(0, adjCenterY, 0);
}
else{
resetCollider();
}
}
}
anim.SetBool("Jump", false);
}
}
else if (currentBaseState.nameHash == idleState)
{
if(useCurves){
resetCollider();
}
if (Input.GetButtonDown("Jump")) {
anim.SetBool("Rest", true);
}
}
else if (currentBaseState.nameHash == restState)
{
if(!anim.IsInTransition(0))
{
anim.SetBool("Rest", false);
Universitas Sumatera Utara
lxii
}
}
}
void resetCollider()
{
col.height = orgColHight;
col.center = orgVectColCenter;
}
}
4.
Untuk Pergerakan Kamera
using UnityEngine;
using System.Collections;
public class ThirdPersonCamera : MonoBehaviour
{
public float smooth = 3f;
Transform standardPos;
Transform frontPos;
Transform jumpPos;
bool bQuickSwitch = false;
void Start()
{
standardPos = GameObject.Find ("CamPos").transform;
if(GameObject.Find ("FrontPos"))
frontPos = GameObject.Find ("FrontPos").transform;
if(GameObject.Find ("JumpPos"))
jumpPos = GameObject.Find ("JumpPos").transform;
transform.position = standardPos.position;
transform.forward = standardPos.forward;
}
void FixedUpdate ()
{
if(Input.GetButton("Fire1"))
{
setCameraPositionFrontView();
}
else if(Input.GetButton("Fire2"))
{
setCameraPositionJumpView();
Universitas Sumatera Utara
lxiii
}
else if(Input.GetKey(KeyCode.Escape))
{
Application.Quit ();
}
else
{
// return the camera to standard position and direction
setCameraPositionNormalView();
}
}
void setCameraPositionNormalView()
{
if(bQuickSwitch == false){
transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.fixed
DeltaTime * smooth);
transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.fixed
DeltaTime * smooth);
}
else{
transform.position = standardPos.position;
transform.forward = standardPos.forward;
bQuickSwitch = false;
}
}
void setCameraPositionFrontView()
{
bQuickSwitch = true;
transform.position = frontPos.position;
transform.forward = frontPos.forward;
}
void setCameraPositionJumpView()
{
bQuickSwitch = false;
transform.position = Vector3.Lerp(transform.position, jumpPos.position, Time.fixedDeltaTi
me * smooth);
transform.forward = Vector3.Lerp(transform.forward, jumpPos.forward, Time.fixedDeltaTim
e * smooth);}
}
Universitas Sumatera Utara
lxiv
B-1
DAFTAR RIWAYAT HIDUP
CURRICULUM VITAE
PERSONAL DATA
Full Name
: Bobby Putra Johan
Nick Name
: Bobby
Place/ Date of Birth
: Medan/April 20rd 1994
Sex
: Male
Religion
: Islam
Nationality
: Indonesia
Address
: Perumnas Simalingkar, Kec, Pancur Batu
Mobile Phone
: 081361115937
: bat_devil5@yahoo.com
Marital Status
: unmarried / married
EDUCATION
Bachelor of Computer Science
University of Sumatera Utara, Medan
2012-Present
Higher Secondary Education
SMK TELKOM SANDHY PUTRA
2008-2011
Universitas Sumatera Utara
lxv
Secondary Education
SMPN 2 MEDAN
2005-2008
Primary Education
SDN 068008 MANGGA
1999-2005
COMPUTER SKILL
Programming : C#, C
Database
: MySQL, Sqlite, Ms. Access
IDE
: Unity, Free Pascal, Dev C++, Arduino ,CV-Avr
Other
:-
PROJECT UNDERTAKEN
Fire fighting Robot
Line Follower Robot
Automatic Locking System
Universitas Sumatera Utara
lxvi
ORGANIZATIONAL EXPERIENCES
No
Organization
Position
Year
1
Sikonek (Sistem kontrol dan elektronika)
Anggota Humas
2013-2014
2
Sikonek (Sistem kontrol dan elektronika)
Koordinator
Litbang(penelitian
dan pengembangan)
2014-2015
3
IKLC USU
Koordinator
2014-2015
Organisasi dan
Arsitektur Komputer
SEMINARS
No. Seminar
Year
1
Si GELITIK
2013
4
SENARAI
2014
“I hereby declare that all information above is true and correct to the best of my knowledge.”
Medan,21 Juli 2017
Bobby Putra Johan
Universitas Sumatera Utara
lxvii
DAFTAR PUSTAKA
Cotta, A. & Devidas, N. T. 2016. Wireless Communication Using HC-05 Bluetooth Module
Interfaced with Arduino. International Journal of Science, Engineering and
Technology Research (IJSETR) 5(4) : 869-872.
Kurniawan, T., Hanafi, L. & Apriliani, E. 2013. Penerapan Metode Filter Kalman dalam
Perbaikan Hasil Prediksi Cuaca dengan Metode ARIMA 1-7.
Kalane, P. & Loni, P. 2012. Target Tracking Using Kalman Filter. International Journal of
Science & Technology 2(2) : 17-24 (Online) www.ijst.co.in.
Quadri, S. A. & Sidek. O. 2014. Error and Noise Analysis in an IMU using Kalman Filter.
International Journal of Hybrid Information Technology 7(3) : 39-48 (Online)
http://dx.doi.org/10/14257/ijhit.2014.7.3.06.
Fahmedha, N,. Prakash, P. C,. Pooja, A,. & Rachana, R. 2015. Estimation of System
Parameters Using Kalman Filter and Extended Kalman Filter. International
Journal of Advanced Technology and Engineering Exploration 2(6): 84-89.
Sierociuk, D. & Dzielinski, A. 2006. Farctional Kalman Filter Algorithm for The State,
Parameters and Order of Fractional System Estimation. International Journal of
Applications Math and Computer Science 16(1): 129-140.
Bassi, S.J,. Mishra, M.K,. & Omizegba, E.E. 2011. Automatic Tuning of ProportionalIntegral-Derivative (PID) Controller using Particle Swarm Optimization (PSO)
Algorithm. International Journal of Artificial Intelligence & Applications (IJAIA)
2(4) : 25-34 (Online) DOI:10.5121/ijaia.2011.2403.
Mon, Y. 2015. The Gyroscope Senssor Test by Using Arduino Platform. International
Journal of Sciencetific and Technology Research (IJSTR) 4(6): 398-400 (Online)
www.ijstr.org
Universitas Sumatera Utara