Skip to content
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

PDS Port #38

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Makefile
.mxproject
startup_stm32f303xe.s
STM32F303RETx_FLASH.ld
__pycache__

# Generated protobuf files
*.pb.cc
Expand Down
3 changes: 3 additions & 0 deletions Examples/UdpSendReceive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## UDP Send / Receive
A simple example of sending and receiving a message using python
annd UDP, to aid in the development of a slim wrapper for python UDP.
9 changes: 9 additions & 0 deletions Examples/UdpSendReceive/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
""" config.py

UDP Configuration values
"""

# UDP Config Values
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
SEND_FREQUENCY = 1
17 changes: 17 additions & 0 deletions Examples/UdpSendReceive/udp_receive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
""" udp_receive.py

Receive and print a message via UDP
"""

import socket

from config import *

if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
data, addr = sock.recvfrom(1024) # Buffer size is 1024 bytes
print(f"Received message: {data}")
25 changes: 25 additions & 0 deletions Examples/UdpSendReceive/udp_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
""" udp_send.py

Send a simple message over UDP.
"""

import socket
import time

from config import *

if __name__ == "__main__":
MESSAGE = b"Hello, World!"

print(f"UDP target IP: {UDP_IP}")
print(f"UDP target port: {UDP_PORT}")
print(f"Message: {MESSAGE}\n")

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP

while True:
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
print("Message sent...\n")
time.sleep(1/SEND_FREQUENCY)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats going on here

98 changes: 98 additions & 0 deletions OnLand/PDS/UDP/udp_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
""" udp_connection.py

Classes:
UDPListener - Wrapper around a uni-directional UDP connection
"""
import logging
import socket
import select
from datetime import timedelta


class UDPListener:
""" UDP Listener

Uni-directional UDP listening on specified ip and port

Fields:
ip: string - connection address
port: int - port to listen on
max_buffer: int - max byte size of the UDP receive/send buffers
timeout: timedelta - how long to poll for data on the socket before timing out
"""

def __init__(self, ip : str, port : int, max_buffer : int, timeout : timedelta):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this typehinting style??

self.sock = None
self.ip = ip
self.port = port
self.max_buffer_size = max_buffer
self.timeout = timeout

@classmethod
def transcribe(self, data : bytes):
""" Log the received byte data """
logging.info(f"[DATA] {data}")

def connect(self):
""" Connect to socket """
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.max_buffer_size)
self.sock.bind((self.ip, self.port))
except Exception as e:
self.disconnect()
raise e

def is_connected(self):
""" Returns true if connected """
connected = self.sock is not None and self.sock.fileno() >= 0
return connected

def disconnect(self):
""" Disconnect from socket """
if self.sock is not None:
self.sock.close()
self.sock = None

def recv(self):
""" Receive UDP packet """
if not self.is_connected():
return None
try:
ready = select.select([self.sock], [], [], self.timeout.total_seconds())
if ready[0]:
data = self.sock.recvfrom(self.max_buffer_size)
if data is not None:
return data[0]
else:
return None

except Exception as e:
self.disconnect()
raise e

def __str__(self):
ret_str = f"""UDP Connection:
\nIp: {self.ip}
\nPort: {self.port}
\nBuffer Size: {self.max_buffer_size}
"""
return ret_str
Comment on lines +75 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice


if __name__ == "__main__":
# Vehicle ECU
VEHICLE_IP = "127.0.0.1"
VEHICLE_UDP_PORT = 5005

con = UDPListener(VEHICLE_IP, VEHICLE_UDP_PORT, 1024, timedelta(seconds=2))
con.connect()

counter = 5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be getting used anywhere

while True:
data = con.recv()
if data is not None:
print(f"Data:{data.decode('utf-8')}")
else:
print("No data...")

10 changes: 10 additions & 0 deletions OnLand/PDS/boring_packet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
""" boring_packet.py

Contains the BoringPacket class for packaging
critical system info for The Boring Company.
"""

class BoringPacket():
""" Create and pack The Boring Struct
"""
raise NotImplementedError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new newline EOF

28 changes: 28 additions & 0 deletions OnLand/PDS/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
""" config.py

Configuration values for:
- InfluxDB
- Vehicle ECU TCP/UDP Connections
- Surface ECU TCP/UDP Connections
"""

# Influx
INFLUX_HOST = "localhost"
INFLUX_PORT = 8086
INFLUX_NAME = "paradigm_boring"
INFLUX_USER = "paradigm"
INFLUX_PW = "boring"

# TBC Packet
TEAM_NAME = "Paradigm Boring"

# SocketIO
SOCKET_SERVER = "http://localhost:5000"

# Vehicle ECU
VEHICLE_IP = "127.0.0.1"
VEHICLE_UDP_PORT = 5005

# Surface ECU
SURFACE_IP = "127.0.0.1"
SURFACE_UDP_PORT = 5006
1 change: 0 additions & 1 deletion OnLand/PDS/placeholder.py

This file was deleted.