Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
walchko committed Dec 11, 2023
1 parent e4b4cd3 commit 958020b
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ poetry.lock
__pycache__
.pytest_cache
old
tmp
9 changes: 9 additions & 0 deletions python/examples/files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"namespace": "foobar",
"license": "MIT Kevin Walchko (c) 2023",
"output": "tmp",
"1": "messages/vec.yivo",
"2": "messages/quat.yivo",
"4": "messages/imu.yivo",
"5": "messages/cal.yivo"
}
8 changes: 8 additions & 0 deletions python/examples/messages/cal.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
float[12] cals
uint8 sensor

<enum types uint8_t
accels = 1
gyros = 2
mags = 4
enum>
18 changes: 18 additions & 0 deletions python/examples/messages/imu.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include vec

<enum error uint32_t
none = 0
accels = 1
gyros = 2
mags = 4
pressure = 8
temperature = 16
enum>

vec accel
vec gyro
vec mag
float pressure
float temperature
uint32 error
uint32 timestamp
26 changes: 26 additions & 0 deletions python/examples/messages/quat.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
float w
float x
float y
float z

fru fru

<c
float magnitude() {
return sqrtf(w*w + x*x + y*y + z*z);
}

vec_t normalize() {
float mag = magnitude();
return quat_t{w/mag,x/mag,y/mag,z/mag};
}
c>

<p
def magnitude(self):
return sqrt(self.x*self.x + self.y*self.y + self.z*self.z)

def normalize(self):
mag = self.magnitude()
return quat_t(self.w/mag, self.x/mag, self.y/mag, self.z/mag)
p>
19 changes: 19 additions & 0 deletions python/examples/messages/vec.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
float x
float y
float z

<c
float magnitude() {
return sqrtf(x*x + y*y + z*z);
}

vec_t normalize() {
float mag = magnitude();
return vec_t{x/mag,y/mag,z/mag};
}
c>

<p
def normalize(self):
return sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
p>
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pytest = "*"
numpy = "*"

[tool.poetry.scripts]
yivo_generate = 'yivo.generator.yivo_gen'
yivo_generate = 'yivo.generator.yivo_gen:run'

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
11 changes: 11 additions & 0 deletions python/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ jerry=32
enum>
```

```json
{
"namespace": "foobar",
"license": "MIT Kevin Walchko (c) 2023",
"output": "test",
"1": "messages/vec.yivo",
"2": "messages/quat.yivo",
"4": "messages/imu.yivo",
"5": "messages/cal.yivo"
}
```

# MIT License

Expand Down
129 changes: 129 additions & 0 deletions python/yivo/generator/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from jinja2 import Environment
from jinja2.loaders import FileSystemLoader
import pathlib
from collections import namedtuple

tmp_dir = pathlib.Path(__file__).resolve().parent/"templates"
env = Environment(loader=FileSystemLoader(tmp_dir))

# py - python name
# fmt - pack/unpack
# complex - for user defined types, more involved
VarInfo = namedtuple("VarInfo","c py size fmt complex")

var_types = {
"uint8": VarInfo("uint8_t", "int",1, "B", False),
"uint16": VarInfo("uint16_t", "int",2, "H", False),
"uint32": VarInfo("uint32_t", "int", 4, "I", False),
"uint64": VarInfo("uint64_t", "int",8, "Q", False),
"int8": VarInfo("int8_t", "int",1, "b", False),
"int16": VarInfo("int16_t", "int", 2, "h", False),
"int32": VarInfo("int32_t", "int", 4, "i", False),
"int64": VarInfo("int64_t", "int", 8, "q", False),
"float": VarInfo("float", "float", 4, "f", False),
"double": VarInfo("double", "float", 8, "d", False)
}

def write_file(filename, content):
with filename.open("w", encoding="utf-8") as fd:
fd.write(content)
print(f"Wrote File: {filename}")

def create_python(msg_parts, out_path):
comments = []
for c in msg_parts.comments:
comments.append(c)

func_args = []
for t, ars, v, c in msg_parts.fields:
func_args.append(v)

includes = []
for i in msg_parts.includes:
ii = f'from {i} import *'
includes.append(ii)

vars = []
for t, ars, v, c in msg_parts.fields:
line = f"{v}: {var_types[t].py}"
if c: line += f" {c}"
vars.append(line)

tmpl = env.get_template("msg.py.jinja")
info = {
"name": msg_parts.file.stem,
"vars": vars,
"includes": includes,
"msg_size": msg_parts.msg_size,
# "msg_size_type": "uint8_t",
"comments": comments,
"args": func_args,
"functions": msg_parts.py_funcs,
"enums": msg_parts.enums,
"format": var_types[ msg_parts.file.stem ].fmt,
"msgid": msg_parts.id,
"license_notice": msg_parts.license_notice
}
content = tmpl.render(info)

filename = msg_parts.file.stem + "_t.py"
filename = out_path/filename
write_file(filename, content)


def create_c_header(msg_parts, out_path):
"""
struct
- vars: string, "int bob[2];"
functions
- func_args: tuple(type, var_name), ("int","bob[2]")
"""
comments = []
for c in msg_parts.comments:
c = c.replace("#", "//")
comments.append(c)

func_args = []
for t, ars, v, c in msg_parts.fields:
if var_types[t].complex: t = var_types[t].c + "&"
else: t = var_types[t].c
if ars > 0:
vv = f"{v}[{ars}]"
else:
vv = f"{v}"

func_args.append((t,vv,v))

includes = []
for i in msg_parts.includes:
ii = f'#include "{i}.h"'
includes.append(ii)

vars = []
for t, ars, v, c in msg_parts.fields:
if ars == 0: line = f"{var_types[t].c} {v};"
else: line = f"{var_types[t].c} {v}[{ars}];"
if c: line += f" {c.replace('#','//')}"
vars.append(line)

tmpl = env.get_template("msg.cpp.jinja")
info = {
"name": msg_parts.file.stem,
"vars": vars,
"includes": includes,
"msg_size": msg_parts.msg_size,
"msg_size_type": "uint8_t",
"comments": comments,
"args": func_args,
"functions": msg_parts.c_funcs,
"enums": msg_parts.enums,
"msgid": msg_parts.id,
"license_notice": msg_parts.license_notice,
"namespace": msg_parts.namespace
}
content = tmpl.render(info)

filename = msg_parts.file.stem + "_t.hpp"
filename = out_path/filename
write_file(filename, content)
62 changes: 62 additions & 0 deletions python/yivo/generator/msg_parts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

from colorama import Fore


class MsgParts:
"""
Breaks a message format appart and stores the results so it can be
converted into other languages. Supported languages:
- python
- C/C++
"""
def __init__(self):
self.comments = [] # comments in body of message prototype
self.fields = [] # variables in message
self.includes = [] # included message headers/modules
self.c_funcs = [] # custom C functions
self.py_funcs = [] # custom Python functions
self.enums = [] # enums
self.msg_size = 0 # size of message in bytes
self.file = None # filename for naming the message
self.id = 0 # message id number
self.namespace = None # cpp namespace

def __repr__(self):
return str(self)

def __str__(self):
ret = f"{Fore.YELLOW}------------------------------\n"
ret += f"File: {self.file}\n"
if self.namespace is not None:
ret += f"Namespace: {self.namespace}\n"
ret += f"------------------------------\n{Fore.RESET}"
ret += f"{Fore.CYAN}Comments:\n{Fore.RESET}"
ret += f"{Fore.GREEN}"
for c in self.comments:
ret += f" {c}\n"
ret += f"{Fore.RESET}"

ret += f"\n{Fore.CYAN}Fields:\n{Fore.RESET}"
for f in self.fields:
ret += f" {f}\n"

ret += f"\n{Fore.CYAN}Python Functions:\n{Fore.RESET}"
for f in self.py_funcs:
ret += f" {f}\n"

ret += f"\n{Fore.CYAN}C Functions:\n{Fore.RESET}"
for f in self.c_funcs:
ret += f" {f}\n"

ret += f"\n{Fore.CYAN}Includes:\n{Fore.RESET}"
ret += f"{Fore.BLUE}"
for i in self.includes:
ret += f" {i}\n"
ret += f"{Fore.RESET}\n"

ret += f"\n{Fore.CYAN}Enums:\n{Fore.RESET}"
for f in self.enums:
ret += f" {f}\n"

ret += f"{Fore.CYAN}\nMessage Size:{Fore.RESET} {self.msg_size}\n"
return ret
Loading

0 comments on commit 958020b

Please sign in to comment.