1. Brian Thorne
  2. python-socket-examples

Commits

Brian Thorne  committed ad346d8

Add python3 bluetooth socket example: iRacer car.

  • Participants
  • Parent commits b357792
  • Branches default

Comments (0)

Files changed (1)

File bluetooth_car.py

View file
+import socket
+import time
+
+class BluetoothCar:
+    def __init__(self, mac_address="00:12:05:09:98:36"):
+        self.socket = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
+        self.socket.connect((mac_address, 1))
+    
+    def _write(self, data_byte):
+        self.socket.send(bytes([data_byte]))
+    
+    def drive(self, command, duration=1.0):
+        self._write(command)
+        time.sleep(duration)
+        self.stop()
+        
+    def forwards(self, duration=1.0):
+        self.drive(0x16, duration)
+        
+    def reverse(self, duration=1.0):
+        self.drive(0x26, duration)
+
+    def left(self, duration=1.0):
+        self.drive(0x57, duration)
+
+    def right(self, duration=1.0):
+        self.drive(0x67, duration)
+
+    def stop(self):
+        self._write(0x00)
+    
+    def __del__(self):
+        self.stop()
+
+if __name__ == "__main__":
+    car = BluetoothCar()
+    while True:
+        car.forwards(0.05)