Created
May 19, 2025 12:54
-
-
Save ajangrahmat/000e4ae81fbb6757fa833d6589250d69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# firaindo.py | |
import time | |
import cv2 | |
import avisengine | |
import config | |
class FiraIndo: | |
def __init__(self): | |
self.car = avisengine.Car() | |
if not self.car.connect(config.SIMULATOR_IP, config.SIMULATOR_PORT): | |
print("Failed to connect to simulator. Program stopped.") | |
exit(1) | |
time.sleep(3) | |
self.car.setSensorAngle(40) | |
self.default_speed = 20 | |
# Set default speed | |
def set_speed(self, speed): | |
self.default_speed = speed | |
# Movement functions | |
def go(self, speed=None): self._drive(speed or self.default_speed, 0) | |
def left(self, speed=None, angle=20): self._drive(speed or 15, -angle) | |
def right(self, speed=None, angle=20): self._drive(speed or 15, angle) | |
def back(self, speed=None): self._drive(-(speed or 15), 0) | |
def stop(self): self._drive(0, 0) | |
def _drive(self, speed, steering): | |
self.car.setSpeed(speed) | |
self.car.setSteering(steering) | |
# Run movement for a specific time | |
def run_for(self, seconds, action): | |
action() | |
start = time.time() | |
while time.time() - start < seconds: | |
self.car.getData() | |
img = self.car.getImage() | |
if img is not None: | |
cv2.imshow('Car Camera', img) | |
if cv2.waitKey(10) == ord('q'): | |
break | |
time.sleep(0.01) | |
# Get camera image | |
def get_image(self): | |
self.car.getData() | |
return self.car.getImage() | |
# Get sensor values [left, middle, right] | |
def get_sensors(self): | |
self.car.getData() | |
return self.car.getSensors() | |
# Get current speed | |
def get_speed(self): | |
return self.car.getSpeed() | |
# Stop and close window | |
def shutdown(self): | |
self.stop() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment