Skip to content

Feat/faisal/addspi #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pyzmq = "^22.0.3"
attrs = "^21.2.0"
loguru = "^0.5.3"
pydoc-markdown = {version = "^3.10.3", optional = true}
spidev = {version = "^3.5", markers = "sys_platform == 'linux'"}

[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
attrs==20.3.0; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.4.0")
attrs==21.2.0; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
cffi==1.14.5; implementation_name == "pypy" and python_version >= "3.6"
colorama==0.4.4; python_version >= "3.5" and python_full_version < "3.0.0" and sys_platform == "win32" or sys_platform == "win32" and python_version >= "3.5" and python_full_version >= "3.5.0"
loguru==0.5.3; python_version >= "3.5"
py==1.10.0; python_version >= "3.6" and python_full_version < "3.0.0" and implementation_name == "pypy" or implementation_name == "pypy" and python_version >= "3.6" and python_full_version >= "3.4.0"
pycparser==2.20; python_version >= "3.6" and python_full_version < "3.0.0" and implementation_name == "pypy" or implementation_name == "pypy" and python_version >= "3.6" and python_full_version >= "3.4.0"
pyzmq==22.0.3; python_version >= "3.6"
spidev==3.5; sys_platform == "linux"
win32-setctime==1.0.3; sys_platform == "win32" and python_version >= "3.5"
42 changes: 33 additions & 9 deletions systems/clients.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from platform import machine
from random import randint
from time import sleep

Expand All @@ -7,6 +8,9 @@

from systems.controllers import Message

if ("armv" in machine()): # if machine is rpi, import spidev
import spidev


class Client(ABC):
core_frontend_address: str
Expand Down Expand Up @@ -76,19 +80,39 @@ def run(self):
return

while True:
dummy_message = Message(controller="MotorController",
data={
"speed": randint(50, 100),
"voltage": randint(20, 40),
"temperature": randint(50, 100)
},
destinations=[['browser', 'ui'],
['canbus']])
self.socket.send_json(dummy_message)
if ("armv" in machine()): # checks if python is running in a rpi
speed = self.get_spi("s")
if (speed == -1):
speed = randint(50, 100)
voltage = self.get_spi("v")
if (voltage == -1):
voltage = randint(50, 100)
else:
speed = randint(50, 100)
voltage = randint(50, 100)
message = Message(controller="MotorController",
data={
"speed": speed,
"voltage": voltage
},
destinations=[['browser', 'ui'], ['canbus']])
self.socket.send_json(message)
incoming_message: Message = self.socket.recv_json()
logger.debug(f"Can Bus received: {incoming_message}")
sleep(1)

def get_spi(self, mode) -> int:
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000
recieved_byte = spi.xfer2([ord('s')])
sleep(0.5)
if ((recieved_byte[0] == 0) or (recieved_byte[0] == 255)):
spi.close()
return -1
spi.close()
return recieved_byte[0]

def connect_to_server(self) -> bool:
self.socket.connect(self.core_frontend_address)
logger.info(f"{self.identity} started, "
Expand Down