Commit 3018ff22 authored by Nicolas Van der Noot's avatar Nicolas Van der Noot
Browse files

hardcoded script added

parent 74618bf9
# takes two argument: the first one is an input c file generated by the old version of Robotran
# the second one is the output c file to generate
# example: python hardcoded_notation.py mbs_dirdyna_my_model.c new_mbs_dirdyna_my_model.c
import sys
# return true if it is a float
def isInt(value):
try:
int(value)
return True
except ValueError:
return False
# get line with new pattern when there is one index
def new_line_simple_index(old_line, old_pattern, new_pattern):
elem_1 = old_line.split('{}['.format(old_pattern))
new_line = elem_1[0]
if len(elem_1) > 1:
for i in range(0, len(elem_1)):
elem_2 = elem_1[i].split(']')
if len(elem_2) > 1:
if isInt(elem_2[0]):
ind = int(elem_2[0])
new_line = '{}{}_{}{}'.format(new_line, new_pattern, ind, elem_2[1])
for j in range(2, len(elem_2)):
new_line = '{}]{}'.format(new_line, elem_2[j])
return new_line
# get line with new pattern when there are two indexes
def new_line_double_index(old_line, old_pattern, new_pattern):
elem_1 = old_line.split('{}['.format(old_pattern))
new_line = elem_1[0]
if len(elem_1) > 1:
for i in range(0, len(elem_1)):
elem_2 = elem_1[i].split('][')
if len(elem_2) > 1:
if isInt(elem_2[0]):
ind_1 = int(elem_2[0])
new_line = '{}{}_{}_'.format(new_line, new_pattern, ind_1)
elem_3 = elem_2[1].split(']')
if len(elem_3) > 1:
if isInt(elem_3[0]):
ind_2 = int(elem_3[0])
new_line = '{}{}{}'.format(new_line, ind_2, elem_3[1])
for j in range(2, len(elem_3)):
new_line = '{}]{}'.format(new_line, elem_3[j])
for j in range(2, len(elem_2)):
new_line = '{}][{}'.format(new_line, elem_2[j])
return new_line
# generate output file
def gen_out_file(in_file, out_file):
out_write = open(out_file,'w')
with open(in_file,'r') as f:
# loop on all the lines
for line in f:
new_line = new_line_simple_index(line, 's->g', 'G')
new_line = new_line_simple_index(new_line, 's->m', 'M')
new_line = new_line_double_index(new_line, 's->l', 'L')
new_line = new_line_double_index(new_line, 's->In', 'IN')
new_line = new_line_double_index(new_line, 's->dpt', 'DPT')
new_line = new_line.replace('s->frc','frc')
new_line = new_line.replace('s->trq','trq')
new_line = new_line.replace('#include "mbs_project_interface.h"', '#include "mbs_project_interface.h"\n#include "user_hardcoded.h"')
new_line = new_line.replace('#define q s->q', '\ndouble *q = s->q;')
new_line = new_line.replace('#define qd s->qd', 'double *qd = s->qd;')
new_line = new_line.replace('#define qdd s->qdd','double *qdd = s->qdd;\n\ndouble **frc = s->frc;\ndouble **trq = s->trq;')
# write line
out_write.write(new_line)
out_write.close()
# main
if len(sys.argv) == 3:
in_file = sys.argv[1]
out_file = sys.argv[2]
gen_out_file(in_file, out_file)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment