Connection a PS4 DuelShock controller to the RaspberryPi in the Spirit rover

After completing the build of Spirit and Opportunity, I am attempting to control the Spirit robot with a PlayStation 4 DuelShock controller.

Enable bluetooth discovery on the Raspberry PI:

pi@raspberrypi:~ $ bluetoothctl
Agent registered
[bluetooth]# agent on
Agent is already registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:8C:38:05 Discoverable: yes
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# scan on

Put the DS controller into pairing mode with the PS and Share buttons

You will see the device pop up on the console:

[CHG] Device 84:17:66:ED:3D:BE RSSI: -54
[bluetooth]# pair 84:17:66:ED:3D:BE
Attempting to pair with 84:17:66:ED:3D:BE
[CHG] Device 84:17:66:ED:3D:BE Connected: yes
[CHG] Device 84:17:66:ED:3D:BE UUIDs: 00001124-0000-1000-8000-00805f9b34fb
[CHG] Device 84:17:66:ED:3D:BE UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device 84:17:66:ED:3D:BE ServicesResolved: yes
[CHG] Device 84:17:66:ED:3D:BE Paired: yes
Pairing successful
Authorize service
[agent] Authorize service 00001124-0000-1000-8000-00805f9b34fb (yes/no):yes
[Wireless Controller]# trust 84:17:66:ED:3D:BE
[CHG] Device 84:17:66:ED:3D:BE Trusted: yes
Changing 84:17:66:ED:3D:BE trust succeeded
[Wireless Controller]# quit

Check the battery level of the controller

pi@raspberrypi:~ $ cat /sys/class/power_supply/sony_controller_battery_84:17:66:ed:3d:be/capacity
90

Ok. I have successfully paired and tested the controller.
Thanks to Sony DualShock - Gentoo wiki for the instructions on this.

I had some interesting times getting this working. When I placed the pi into scanning mode, I attempted to pair with so many devices before finding the PS4DS controller.

could you tell me how to do it? where to add the code? please