Rename folder
This commit is contained in:
parent
651713c081
commit
daef7c97e4
11 changed files with 5 additions and 5 deletions
35
compLib/ADC.py
Normal file
35
compLib/ADC.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import smbus
|
||||
|
||||
SINGLE_ENDED = 0x84
|
||||
ADDRESS = 0x48
|
||||
|
||||
bus = smbus.SMBus(1)
|
||||
|
||||
|
||||
class ADC:
|
||||
|
||||
@staticmethod
|
||||
def read(channel) -> float:
|
||||
"""
|
||||
Read from adc channel
|
||||
0 -> Left IR Sensor
|
||||
1 -> Right IR Sensor
|
||||
2 -> Battery voltage / 3
|
||||
:param channel: Channel between 0 and 2
|
||||
:return: voltage
|
||||
"""
|
||||
|
||||
"""Select the Command data from the given provided value above"""
|
||||
command = SINGLE_ENDED | ((((channel << 2) | (channel >> 1)) & 0x07) << 4)
|
||||
bus.write_byte(ADDRESS, command)
|
||||
|
||||
while "Koka is great":
|
||||
value1 = bus.read_byte(ADDRESS)
|
||||
value2 = bus.read_byte(ADDRESS)
|
||||
if value1 == value2:
|
||||
break
|
||||
|
||||
voltage = value1 / 255.0 * 3.3 # calculate the voltage value
|
||||
voltage = round(voltage, 2)
|
||||
|
||||
return voltage
|
51
compLib/Api.py
Normal file
51
compLib/Api.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
API_URL = os.getenv("API_URL", "http://localhost:5000/")
|
||||
API_URL_GET_POS = API_URL + "getPos"
|
||||
API_URL_GET_OP = API_URL + "getOp"
|
||||
API_URL_GET_GOAL = API_URL + "getGoal"
|
||||
API_URL_GET_ITEMS = API_URL + "getItems"
|
||||
|
||||
|
||||
class Seeding:
|
||||
@staticmethod
|
||||
def get_park():
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def pay_park():
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def simon_says():
|
||||
pass
|
||||
|
||||
|
||||
class Position:
|
||||
def __init__(self, x, y, rotation):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.rotation = rotation
|
||||
|
||||
|
||||
class DoubleElim:
|
||||
@staticmethod
|
||||
def get_position():
|
||||
response = json.loads(requests.get(API_URL_GET_POS).content)
|
||||
return Position(response["x"], response["y"], response["degrees"])
|
||||
|
||||
@staticmethod
|
||||
def get_opponent():
|
||||
response = json.loads(requests.get(API_URL_GET_OP).content)
|
||||
return Position(response["x"], response["y"], response["degrees"])
|
||||
|
||||
@staticmethod
|
||||
def get_goal():
|
||||
response = json.loads(requests.get(API_URL_GET_GOAL).content)
|
||||
return Position(response["x"], response["y"], -1)
|
||||
|
||||
@staticmethod
|
||||
def get_items():
|
||||
return json.loads(requests.get(API_URL_GET_ITEMS).content)
|
24
compLib/Battery.py
Normal file
24
compLib/Battery.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from compLib.ADC import ADC
|
||||
|
||||
BATTERY_CHANNEL = 2
|
||||
BATTERY_COUNT = 2
|
||||
BATTERY_MULTIPLIER = 3
|
||||
BATTERY_MIN_VOLTAGE = 3.6
|
||||
BATTERY_MAX_VOLTAGE = 4.1
|
||||
|
||||
adc = ADC()
|
||||
|
||||
|
||||
class Battery(object):
|
||||
"""Used to interact with the battery
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def percent() -> int:
|
||||
"""Get battery percentage
|
||||
|
||||
:return: Percentage between 0 and 100
|
||||
:rtype: int
|
||||
"""
|
||||
voltage = adc.read(BATTERY_CHANNEL) * BATTERY_MULTIPLIER
|
||||
return int((voltage - BATTERY_MIN_VOLTAGE) / BATTERY_MAX_VOLTAGE * 100)
|
28
compLib/Buzzer.py
Normal file
28
compLib/Buzzer.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import atexit
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
GPIO.setwarnings(False)
|
||||
Buzzer_Pin = 17
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(Buzzer_Pin, GPIO.OUT)
|
||||
|
||||
|
||||
class Buzzer:
|
||||
"""Used to interact with the buzzer
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def set(on: bool):
|
||||
"""Turn the buzzer on / off
|
||||
|
||||
:param on: True if on, False if off
|
||||
"""
|
||||
GPIO.output(Buzzer_Pin, on)
|
||||
|
||||
@staticmethod
|
||||
def exit():
|
||||
Buzzer.set(0)
|
||||
|
||||
|
||||
atexit.register(Buzzer.exit)
|
73
compLib/IRSensor.py
Normal file
73
compLib/IRSensor.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
import RPi.GPIO as GPIO
|
||||
|
||||
from compLib.ADC import ADC
|
||||
|
||||
TOP_LEFT_CHANNEL = 0
|
||||
TOP_RIGHT_CHANNEL = 1
|
||||
|
||||
TOP_IR_MIN_VOLTAGE = 0.0
|
||||
TOP_IR_MAX_VOLTAGE = 3.3
|
||||
|
||||
BOTTOM_LEFT_PIN = 14
|
||||
BOTTOM_MIDDLE_PIN = 15
|
||||
BOTTOM_RIGHT_PIN = 23
|
||||
|
||||
adc = ADC()
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
GPIO.setup(BOTTOM_LEFT_PIN, GPIO.IN)
|
||||
GPIO.setup(BOTTOM_MIDDLE_PIN, GPIO.IN)
|
||||
GPIO.setup(BOTTOM_RIGHT_PIN, GPIO.IN)
|
||||
|
||||
|
||||
class IRSensor(object):
|
||||
"""Access the different IR Sensors at top / bottom of the robot
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def top_left_percent() -> int:
|
||||
"""Get top left infrared sensor percentage
|
||||
|
||||
:return: Percentage between 0 and 100
|
||||
:rtype: int
|
||||
"""
|
||||
voltage = adc.read(TOP_LEFT_CHANNEL)
|
||||
return int((voltage - TOP_IR_MIN_VOLTAGE) / TOP_IR_MAX_VOLTAGE * 100)
|
||||
|
||||
@staticmethod
|
||||
def top_right_percent() -> int:
|
||||
"""Get top right infrared sensor percentage
|
||||
|
||||
:return: Percentage between 0 and 100
|
||||
:rtype: int
|
||||
"""
|
||||
voltage = adc.read(TOP_RIGHT_CHANNEL)
|
||||
return int((voltage - TOP_IR_MIN_VOLTAGE) / TOP_IR_MAX_VOLTAGE * 100)
|
||||
|
||||
@staticmethod
|
||||
def bottom_left() -> bool:
|
||||
"""Get bottom left infrared sensor status
|
||||
|
||||
:return: True, if sensor detects IR signals
|
||||
:rtype: bool
|
||||
"""
|
||||
return GPIO.input(BOTTOM_LEFT_PIN)
|
||||
|
||||
@staticmethod
|
||||
def bottom_middle() -> bool:
|
||||
"""Get bottom middle infrared sensor status
|
||||
|
||||
:return: True, if sensor detects IR signals
|
||||
:rtype: bool
|
||||
"""
|
||||
return GPIO.input(BOTTOM_MIDDLE_PIN)
|
||||
|
||||
@staticmethod
|
||||
def bottom_right() -> bool:
|
||||
"""Get bottom right infrared sensor status
|
||||
|
||||
:return: True, if sensor detects IR signals
|
||||
:rtype: bool
|
||||
"""
|
||||
return GPIO.input(BOTTOM_RIGHT_PIN)
|
51
compLib/Motor.py
Normal file
51
compLib/Motor.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import atexit
|
||||
|
||||
from compLib.PCA9685 import PCA9685
|
||||
|
||||
pwm = PCA9685(0x40, debug=True)
|
||||
pwm.setPWMFreq(50)
|
||||
|
||||
MOTOR_COUNT = 4
|
||||
MAX_MOTOR_SPEED = 4095.0
|
||||
MOTOR_PERCENTAGE_MULT = MAX_MOTOR_SPEED / 100.0
|
||||
|
||||
|
||||
class Motor(object):
|
||||
"""Class used to control the motors
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def power(port: int, percent: int):
|
||||
"""Set specified motor to percentage power
|
||||
|
||||
:param port: Port, which the motor is connected to. 0-3, 0 -> top left, 3 -> top right
|
||||
:param percent: Percentage of max speed. between -100 and 100
|
||||
"""
|
||||
forward = True
|
||||
if percent < 0:
|
||||
percent = abs(percent)
|
||||
forward = False
|
||||
|
||||
# bottom left motor is inverted - REEEEEEEEEEEE
|
||||
if port == 1:
|
||||
forward = not forward
|
||||
|
||||
adjusted_speed = int(min(max(0, percent), 100) * MOTOR_PERCENTAGE_MULT)
|
||||
|
||||
if forward:
|
||||
pwm.setMotorPwm(port * 2, 0)
|
||||
pwm.setMotorPwm(port * 2 + 1, adjusted_speed)
|
||||
else:
|
||||
pwm.setMotorPwm(port * 2, adjusted_speed)
|
||||
pwm.setMotorPwm(port * 2 + 1, 0)
|
||||
|
||||
@staticmethod
|
||||
def all_off():
|
||||
"""
|
||||
Turns of all motors
|
||||
"""
|
||||
for i in range(0, MOTOR_COUNT):
|
||||
Motor.power(i, 0)
|
||||
|
||||
|
||||
atexit.register(Motor.all_off)
|
77
compLib/PCA9685.py
Normal file
77
compLib/PCA9685.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import math
|
||||
import time
|
||||
|
||||
import smbus
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Raspi PCA9685 16-Channel PWM Servo Driver
|
||||
# ============================================================================
|
||||
|
||||
class PCA9685:
|
||||
# Registers/etc.
|
||||
__SUBADR1 = 0x02
|
||||
__SUBADR2 = 0x03
|
||||
__SUBADR3 = 0x04
|
||||
__MODE1 = 0x00
|
||||
__PRESCALE = 0xFE
|
||||
__LED0_ON_L = 0x06
|
||||
__LED0_ON_H = 0x07
|
||||
__LED0_OFF_L = 0x08
|
||||
__LED0_OFF_H = 0x09
|
||||
__ALLLED_ON_L = 0xFA
|
||||
__ALLLED_ON_H = 0xFB
|
||||
__ALLLED_OFF_L = 0xFC
|
||||
__ALLLED_OFF_H = 0xFD
|
||||
|
||||
def __init__(self, address=0x40, debug=False):
|
||||
self.bus = smbus.SMBus(1)
|
||||
self.address = address
|
||||
self.debug = debug
|
||||
self.write(self.__MODE1, 0x00)
|
||||
|
||||
def write(self, reg, value):
|
||||
"Writes an 8-bit value to the specified register/address"
|
||||
self.bus.write_byte_data(self.address, reg, value)
|
||||
|
||||
def read(self, reg):
|
||||
"Read an unsigned byte from the I2C device"
|
||||
result = self.bus.read_byte_data(self.address, reg)
|
||||
return result
|
||||
|
||||
def setPWMFreq(self, freq):
|
||||
"Sets the PWM frequency"
|
||||
prescaleval = 25000000.0 # 25MHz
|
||||
prescaleval /= 4096.0 # 12-bit
|
||||
prescaleval /= float(freq)
|
||||
prescaleval -= 1.0
|
||||
prescale = math.floor(prescaleval + 0.5)
|
||||
|
||||
oldmode = self.read(self.__MODE1)
|
||||
newmode = (oldmode & 0x7F) | 0x10 # sleep
|
||||
self.write(self.__MODE1, newmode) # go to sleep
|
||||
self.write(self.__PRESCALE, int(math.floor(prescale)))
|
||||
self.write(self.__MODE1, oldmode)
|
||||
time.sleep(0.005)
|
||||
self.write(self.__MODE1, oldmode | 0x80)
|
||||
|
||||
def setPWM(self, channel, on, off):
|
||||
"Sets a single PWM channel"
|
||||
self.write(self.__LED0_ON_L + 4 * channel, on & 0xFF)
|
||||
self.write(self.__LED0_ON_H + 4 * channel, on >> 8)
|
||||
self.write(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
|
||||
self.write(self.__LED0_OFF_H + 4 * channel, off >> 8)
|
||||
|
||||
def setMotorPwm(self, channel, duty):
|
||||
self.setPWM(channel, 0, duty)
|
||||
|
||||
def setServoPulse(self, channel, pulse):
|
||||
"Sets the Servo Pulse,The PWM frequency must be 50HZ"
|
||||
pulse = pulse * 4096 / 20000 # PWM frequency is 50HZ,the period is 20000us
|
||||
self.setPWM(channel, 0, int(pulse))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
30
compLib/Servo.py
Normal file
30
compLib/Servo.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
from compLib.PCA9685 import PCA9685
|
||||
|
||||
pwm = PCA9685(0x40, debug=True)
|
||||
pwm.setPWMFreq(50)
|
||||
|
||||
|
||||
class Servo:
|
||||
"""Control the servo ports on the robot
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def set_position(channel: int, angle: int):
|
||||
"""Set position of servo connected to port
|
||||
|
||||
:param channel: channel between 0 and 7
|
||||
:param angle: Angle of servo
|
||||
"""
|
||||
angle = abs(angle)
|
||||
|
||||
if channel == 0:
|
||||
pwm.setServoPulse(8 + channel, 2500 - int(angle / 0.09))
|
||||
elif channel < 8:
|
||||
pwm.setServoPulse(8 + channel, 500 - int(angle / 0.09))
|
||||
|
||||
@staticmethod
|
||||
def setup_position():
|
||||
"""Set position of servos to the position used during the setup process
|
||||
"""
|
||||
pwm.setServoPulse(8, 1500)
|
||||
pwm.setServoPulse(9, 1500)
|
106
compLib/Ultrasound.py
Normal file
106
compLib/Ultrasound.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
import time
|
||||
from Motor import *
|
||||
import RPi.GPIO as GPIO
|
||||
from servo import *
|
||||
from PCA9685 import PCA9685
|
||||
|
||||
|
||||
class Ultrasonic:
|
||||
def __init__(self):
|
||||
GPIO.setwarnings(False)
|
||||
self.trigger_pin = 27
|
||||
self.echo_pin = 22
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(self.trigger_pin, GPIO.OUT)
|
||||
GPIO.setup(self.echo_pin, GPIO.IN)
|
||||
|
||||
def send_trigger_pulse(self):
|
||||
GPIO.output(self.trigger_pin, True)
|
||||
time.sleep(0.00015)
|
||||
GPIO.output(self.trigger_pin, False)
|
||||
|
||||
def wait_for_echo(self, value, timeout):
|
||||
count = timeout
|
||||
while GPIO.input(self.echo_pin) != value and count > 0:
|
||||
count = count - 1
|
||||
|
||||
def get_distance(self):
|
||||
distance_cm = [0, 0, 0, 0, 0]
|
||||
for i in range(3):
|
||||
self.send_trigger_pulse()
|
||||
self.wait_for_echo(True, 10000)
|
||||
start = time.time()
|
||||
self.wait_for_echo(False, 10000)
|
||||
finish = time.time()
|
||||
pulse_len = finish - start
|
||||
distance_cm[i] = pulse_len / 0.000058
|
||||
distance_cm = sorted(distance_cm)
|
||||
return int(distance_cm[2])
|
||||
|
||||
def run_motor(self, L, M, R):
|
||||
if (L < 30 and M < 30 and R < 30) or M < 30:
|
||||
self.PWM.setMotorModel(-1450, -1450, -1450, -1450)
|
||||
time.sleep(0.1)
|
||||
if L < R:
|
||||
self.PWM.setMotorModel(1450, 1450, -1450, -1450)
|
||||
else:
|
||||
self.PWM.setMotorModel(-1450, -1450, 1450, 1450)
|
||||
elif L < 30 and M < 30:
|
||||
PWM.setMotorModel(1500, 1500, -1500, -1500)
|
||||
elif R < 30 and M < 30:
|
||||
PWM.setMotorModel(-1500, -1500, 1500, 1500)
|
||||
elif L < 20:
|
||||
PWM.setMotorModel(2000, 2000, -500, -500)
|
||||
if L < 10:
|
||||
PWM.setMotorModel(1500, 1500, -1000, -1000)
|
||||
elif R < 20:
|
||||
PWM.setMotorModel(-500, -500, 2000, 2000)
|
||||
if R < 10:
|
||||
PWM.setMotorModel(-1500, -1500, 1500, 1500)
|
||||
else:
|
||||
self.PWM.setMotorModel(600, 600, 600, 600)
|
||||
|
||||
def run(self):
|
||||
self.PWM = Motor()
|
||||
self.pwm_S = Servo()
|
||||
for i in range(30, 151, 60):
|
||||
self.pwm_S.setServoPwm('0', i)
|
||||
time.sleep(0.2)
|
||||
if i == 30:
|
||||
L = self.get_distance()
|
||||
elif i == 90:
|
||||
M = self.get_distance()
|
||||
else:
|
||||
R = self.get_distance()
|
||||
while True:
|
||||
for i in range(90, 30, -60):
|
||||
self.pwm_S.setServoPwm('0', i)
|
||||
time.sleep(0.2)
|
||||
if i == 30:
|
||||
L = self.get_distance()
|
||||
elif i == 90:
|
||||
M = self.get_distance()
|
||||
else:
|
||||
R = self.get_distance()
|
||||
self.run_motor(L, M, R)
|
||||
for i in range(30, 151, 60):
|
||||
self.pwm_S.setServoPwm('0', i)
|
||||
time.sleep(0.2)
|
||||
if i == 30:
|
||||
L = self.get_distance()
|
||||
elif i == 90:
|
||||
M = self.get_distance()
|
||||
else:
|
||||
R = self.get_distance()
|
||||
self.run_motor(L, M, R)
|
||||
|
||||
|
||||
ultrasonic = Ultrasonic()
|
||||
# Main program logic follows:
|
||||
if __name__ == '__main__':
|
||||
print('Program is starting ... ')
|
||||
try:
|
||||
ultrasonic.run()
|
||||
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
|
||||
PWM.setMotorModel(0, 0, 0, 0)
|
||||
ultrasonic.pwm_S.setServoPwm('0', 90)
|
1
compLib/__init__.py
Normal file
1
compLib/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
__version__ = "0.0.1"
|
Reference in a new issue