Skip to content

Commit

Permalink
migrating away from htcondor
Browse files Browse the repository at this point in the history
  • Loading branch information
grg2rsr committed Jan 21, 2020
1 parent e83fd10 commit eafe8a3
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 45 deletions.
23 changes: 19 additions & 4 deletions 2do.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
# 2do

now: first is to make sure it runs again - checkout
raw interface sits fixed on serial1

[ ] multiple parameter passing to bonsai
make it possible to pass com port to bonsai sketch

implement Pathlib widely - use this chance also to make use of the tmp folder functionality?
## decisions:
tmp folder or not?
pro: cleaner
contra: temporary arduino_vars file also needed

distribute interface generator or not?
pro: makes codebase for tasks cleaner
contra: removes flexibility as interface needs to be freezed
solution: interface generator could have diff versions, could be specified in task config. Could actually be something in task config that defines if raw, where serial etc.

huge pro is though that it will greatly remove clutter

remember that there was a problem, the settable return type (two diff parsers)

## known hardcodes
raw interface sits fixed on serial1
history of serial monitor is limited



Expand All @@ -20,7 +36,6 @@ write in header day of training, version of TaskControl, date of generation etc

## interface generator
+ template and generator is to be distributed with task control actually

make function with settable return type!

## general
Expand Down
53 changes: 37 additions & 16 deletions interface_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# python me to generate an interface.cpp based on the variables in `init_variables.h`
# MAJOR FIXME this file contains unix specific stuff ...

import pandas as pd
import scipy as sp
Expand Down Expand Up @@ -109,31 +110,37 @@ def run(variables_path):
"""

bool_setter_template = """
if (dtype == "bool") {
if (strcmp(varname,"VARNAME")==0){
if (strcmp(varvalue,"false")==0) {
VARNAME = false;
}
else {
VARNAME = true;
}
if (strcmp(varname,"VARNAME")==0){
if (strcmp(varvalue,"false")==0) {
VARNAME = false;
}
else {
VARNAME = true;
}
}
"""

int_setter_template = """
if (dtype == "int") {
if (strcmp(varname,"VARNAME")==0){
VARNAME = atoi(varvalue);
}
if (strcmp(varname,"VARNAME")==0){
VARNAME = atoi(varvalue);
}
"""

long_setter_template = """
if (strcmp(varname,"VARNAME")==0){
VARNAME = atol(varvalue);
}
"""

unsigned_long_setter_template = """
if (strcmp(varname,"VARNAME")==0){
VARNAME = strtoul(varvalue,NULL,10);
}
"""

float_setter_template = """
if (dtype == "float") {
if (strcmp(varname,"VARNAME")==0){
VARNAME = atof(varvalue);
}
if (strcmp(varname,"VARNAME")==0){
VARNAME = atof(varvalue);
}
"""

Expand Down Expand Up @@ -163,6 +170,20 @@ def run(variables_path):
# no ints found
pass

try:
for i, row in init_vars.groupby('dtype').get_group('long').iterrows():
all_setters.append(long_setter_template.replace("VARNAME",row['name']))
except KeyError:
# no longs found
pass

try:
for i, row in init_vars.groupby('dtype').get_group('unsigned long').iterrows():
all_setters.append(unsigned_long_setter_template.replace("VARNAME",row['name']))
except KeyError:
# no unsigned longs found
pass

try:
for i, row in init_vars.groupby('dtype').get_group('float').iterrows():
all_setters.append(float_setter_template.replace("VARNAME",row['name']))
Expand Down
30 changes: 5 additions & 25 deletions interface_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ boolean newData = false;
bool verbose = true;
bool run = false;

int current_state = 1;

void getSerialData() {
// check if command data is available and if yes read it
// all commands are flanked by <>
Expand Down Expand Up @@ -103,26 +105,9 @@ void processSerialData() {
char varvalue[len-split+1];
strlcpy(varvalue, line+split+1, len-split+1);

// parse dtype
String dtype = "unset";

// test for bool
if ((dtype == "true") || (dtype == "false")) {
dtype = "bool";
}

// test for float (has decimal point)
unsigned int num_len = sizeof(varvalue)/sizeof(char);
for (unsigned int i = 0; i < num_len; i++) {
if (varvalue[i] == '.') {
// isFloat = true;
dtype = "float";
}
}

// else must be int
if (dtype == "unset"){
dtype = "int";
// for the state machine "force state" buttons
if (strcmp(varname,"current_state")==0){
current_state = atoi(varvalue);
}

// INSERT_SETTERS
Expand All @@ -148,11 +133,6 @@ void processSerialData() {
}
}

// RAW
if (strcmp(mode,"RAW")==0){
// manually implement functions here
}

newData = false;
}
}
205 changes: 205 additions & 0 deletions old/interface_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# python me to generate an interface.cpp based on the variables in `init_variables.h`

import pandas as pd
import scipy as sp
import sys,os

dtype_map = {
'int':'i4',
'unsigned int':'u4',
'long':'i8',
'unsigned long':'u8',
'bool':'?',
'float':'f4',
'double':'f8',
}

# def parse_arduino_vars(path):
# """ a hacky parser """ # FIXME this needs a new name as well
# with open(path, 'r') as fH:
# lines = fH.readlines()
# lines = [line.strip() for line in lines]

# # hacky parser:
# dfs = []
# for line in lines:
# line = line.strip()
# # to skip
# if line == '':
# continue
# if '*' in line: # in block comment
# continue
# if line[:2] == '//': # full line comment
# continue
# if '//' in line: # remove everything after comment
# line = line.split('//')[0]
# print(line)

# line = line.strip()
# try:
# elements, value = line.split('=')
# value = value[:-1].strip()
# elements = elements.strip().split(' ')
# elements = [elem.strip() for elem in elements]
# name = elements[-1]
# if elements[0] == 'const':
# const = True
# dtype = ' '.join(elements[1:-1])
# else:
# const = False
# dtype = ' '.join(elements[:-1])
# value = sp.array(value, dtype=dtype_map[dtype])
# dfs.append(pd.DataFrame([[name, value, dtype, const]],columns=['name', 'value', 'dtype', 'const']))
# except:
# print('unreadable line: ',line)
# pass
# arduino_vars = pd.concat(dfs, axis=0)
# arduino_vars = arduino_vars.reset_index(drop=True)

# return arduino_vars

def parse_arduino_vars(path):
""" a kind of hacky parser for init_variables.h """
with open(path, 'r') as fH:
lines = fH.readlines()
lines = [line.strip() for line in lines]

# hacky parser:
dfs = []
for line in lines:
line = line.strip()
# to skip
if line == '':
continue
if '*' in line: # in block comment
continue
if line[:2] == '//': # full line comment
continue
if '//' in line: # remove everything after comment
line = line.split('//')[0]
print(line)

line = line.strip()
try:
elements, value = line.split('=')
value = value[:-1].strip()
elements = elements.strip().split(' ')
elements = [elem.strip() for elem in elements]
name = elements[-1]
dtype = ' '.join(elements[:-1])
value = sp.array(value, dtype=dtype_map[dtype])
# FIXME hardcoded dtype instead of dtypemap[dtype] (as in utils), in essence, same func, different behavior. will confuse ppl
dfs.append(pd.DataFrame([[name, value, dtype]],columns=['name', 'value', 'dtype']))
except:
print('unreadable line: ',line)
pass
arduino_vars = pd.concat(dfs, axis=0)
arduino_vars = arduino_vars.reset_index(drop=True)

return arduino_vars


def run(variables_path):
init_vars = parse_arduino_vars(variables_path)

getter_template = """
if (strcmp(varname,"VARNAME")==0){
Serial.println(String(varname)+String("=")+String(VARNAME));
}
"""

bool_setter_template = """
if (dtype == "bool") {
if (strcmp(varname,"VARNAME")==0){
if (strcmp(varvalue,"false")==0) {
VARNAME = false;
}
else {
VARNAME = true;
}
}
}
"""

int_setter_template = """
if (dtype == "int") {
if (strcmp(varname,"VARNAME")==0){
VARNAME = atoi(varvalue);
}
}
"""

float_setter_template = """
if (dtype == "float") {
if (strcmp(varname,"VARNAME")==0){
VARNAME = atof(varvalue);
}
}
"""


# make getters
all_getters = []
all_varnames = []
for i,row in init_vars.iterrows():
all_varnames.append(row['name'])

for i in range(len(all_varnames)):
all_getters.append(getter_template.replace("VARNAME",all_varnames[i]))

# make setters
all_setters = []
try:
for i, row in init_vars.groupby('dtype').get_group('bool').iterrows():
all_setters.append(bool_setter_template.replace("VARNAME",row['name']))
except KeyError:
# no bools found
pass

try:
for i, row in init_vars.groupby('dtype').get_group('int').iterrows():
all_setters.append(int_setter_template.replace("VARNAME",row['name']))
except KeyError:
# no ints found
pass

try:
for i, row in init_vars.groupby('dtype').get_group('float').iterrows():
all_setters.append(float_setter_template.replace("VARNAME",row['name']))
except KeyError:
# no floats ...
pass

with open("interface_template.cpp",'r') as fH:
lines = fH.readlines()

# replace in include line
for i, line in enumerate(lines):
if line == "#include \"interface_variables.h\"\n":
insertion_ind = i
lines[insertion_ind] = "#include \""+variables_path+"\"\n"

# insert getters
for i,line in enumerate(lines):
if line == ' // INSERT_GETTERS\n':
getter_insertion_ind = i
lines.insert(getter_insertion_ind+1,''.join(all_getters))

# insert setters
for i,line in enumerate(lines):
if line == ' // INSERT_SETTERS\n':
setter_insertion_ind = i
lines.insert(setter_insertion_ind+1,''.join(all_setters))

# write all
with open('interface.cpp','w') as fH:
fH.writelines(''.join(lines))

if __name__== "__main__":
if len(sys.argv)==1:
# for using defaults from the cmd
run("interface_variables.h")

if len(sys.argv) == 2:
variables_path = sys.argv[1]
run(variables_path)
Loading

0 comments on commit eafe8a3

Please sign in to comment.