add lookup table
This commit is contained in:
parent
44d4f855c6
commit
535194bcb4
|
@ -15,7 +15,7 @@ CONTIKI_WITH_IPV6 = 1
|
|||
|
||||
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
|
||||
|
||||
PROJECT_SOURCEFILES += ${SKETCH}.cpp
|
||||
PROJECT_SOURCEFILES += ${SKETCH}.cpp klin.c
|
||||
|
||||
# automatically build RESTful resources
|
||||
REST_RESOURCES_DIR = ./resources
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
extern "C" {
|
||||
#include "rest-engine.h"
|
||||
#include "dev/servo.h"
|
||||
#include "klin.h" // linera values lookup table
|
||||
|
||||
extern resource_t res_poti, res_battery;
|
||||
uint8_t poti_pin = A5;
|
||||
|
@ -54,6 +55,7 @@ void loop (void)
|
|||
int value;
|
||||
poti_voltage = analogRead(poti_pin);
|
||||
value=scale (345,868,servo_min,servo_max,poti_voltage);
|
||||
servo_set(1,value);
|
||||
uint16_t lookup_Daumengas(uint16_t i);
|
||||
servo_set(1,lookup_Daumengas(value));
|
||||
printf("%d,%d\n",poti_voltage,value);
|
||||
}
|
||||
|
|
1040
examples/osd/arduino-roller/tools/1.c
Normal file
1040
examples/osd/arduino-roller/tools/1.c
Normal file
File diff suppressed because it is too large
Load diff
15
examples/osd/arduino-roller/tools/1.h
Normal file
15
examples/osd/arduino-roller/tools/1.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef 1_h_
|
||||
#define 1_h_
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
/**
|
||||
* autogenerated lookup function for MyFavoriteSensor
|
||||
* @param i x value
|
||||
* @return f(i), or 6666 if i is out of range
|
||||
*/
|
||||
uint16_t lookup_MyFavoriteSensor(uint16_t i);
|
||||
|
||||
#endif /* 1_h_ */
|
||||
|
15
examples/osd/arduino-roller/tools/README
Normal file
15
examples/osd/arduino-roller/tools/README
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
|
||||
Hallo ,
|
||||
|
||||
Für ein ähnliches Problem habe ich einen csv zu C Konverter in Python
|
||||
geschrieben.
|
||||
Aus dem csv im Anhang habe ich mit
|
||||
|
||||
python lookuptable.py example.csv 1.h 1.c
|
||||
|
||||
das .c und .h erzeugt. Ohne Gewähr, evtl. hilft es.
|
||||
|
||||
link:
|
||||
|
||||
http://www.mikrocontroller.net/topic/232722
|
1029
examples/osd/arduino-roller/tools/example.csv
Normal file
1029
examples/osd/arduino-roller/tools/example.csv
Normal file
File diff suppressed because it is too large
Load diff
BIN
examples/osd/arduino-roller/tools/example.ods
Normal file
BIN
examples/osd/arduino-roller/tools/example.ods
Normal file
Binary file not shown.
176
examples/osd/arduino-roller/tools/lookuptable.py
Normal file
176
examples/osd/arduino-roller/tools/lookuptable.py
Normal file
|
@ -0,0 +1,176 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
|
||||
"""
|
||||
Benutzung:
|
||||
python lookuptable.py bla.csv bla.h bla.c
|
||||
liest bla.csv aus, interpoliert das ganze und schreibt eine tabelle in bla.c und bla.h
|
||||
Achtung, bla.c und bla.h werden brutal ueberschrieben!
|
||||
|
||||
Das csv muss tabs, kommas oder semikolons als trenner haben und folgende felder enthalten:
|
||||
|
||||
#LowValue
|
||||
wird als y fuer alle x-werte, die kleiner als der kleinste x-Wert im csv sind, benutzt
|
||||
#HighValue
|
||||
wird als y fuer alle x-werte, die groesser als der groesste x-Wert im csv sind, benutzt
|
||||
#DataType
|
||||
Datentyp der y-Werte (uint16_t oder uint8_t)
|
||||
#IndexType
|
||||
Datentyp der x-Werte (uint16_t oder uint8_t)
|
||||
#ArrayName
|
||||
Name des Arrays
|
||||
|
||||
Siehe example.csv!!
|
||||
|
||||
|
||||
Benutzung auf eigene Gefahr.
|
||||
"""
|
||||
|
||||
|
||||
import fileinput
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
|
||||
# bei bedarf ergaezen.
|
||||
AdditionalIncludes = [ '<avr/pgmspace.h>', '<inttypes.h>' ]
|
||||
|
||||
|
||||
def create_printable_table(table, dataType, arrayName):
|
||||
""" returns a string with the table in *.c format """
|
||||
result = '/** autogenerated lookup table */\n'
|
||||
result += 'const ' + dataType + " g_LOOKUP_" + arrayName + '[] PROGMEM =\n'
|
||||
result += '{\n'
|
||||
for x in sorted(table.keys()):
|
||||
y = table[x]
|
||||
result += ' /* ' + repr(x).rjust(12) + ' */ ' + repr(y).rjust(12) + ',\n'
|
||||
result += '};'
|
||||
return result
|
||||
|
||||
|
||||
def create_lookup_function(dataType, indexType, arrayName, biggestKey, highValue):
|
||||
header = ''
|
||||
body = ''
|
||||
|
||||
header += '/**\n'
|
||||
header += ' * autogenerated lookup function for ' + arrayName + "\n"
|
||||
header += ' * @param i x value\n'
|
||||
header += ' * @return f(i), or ' + str(highValue) + ' if i is out of range\n'
|
||||
header += ' */\n'
|
||||
header += dataType + ' lookup_' + arrayName + '(' + indexType + ' i);'
|
||||
|
||||
body += dataType + ' lookup_' + arrayName + '(' + indexType + ' i)\n'
|
||||
body += '{\n'
|
||||
body += ' if (i <= ' + str(biggestKey) + ')\n'
|
||||
body += ' return '
|
||||
if dataType == 'uint16_t':
|
||||
body += 'pgm_read_word(&g_LOOKUP_' + arrayName + '[i]);\n'
|
||||
elif dataType == 'uint8_t':
|
||||
body += 'pgm_read_byte(&g_LOOKUP_' + arrayName + '[i]);\n'
|
||||
else:
|
||||
body += 'ERROR: no suitable datatype\n'
|
||||
body += ' else\n'
|
||||
body += ' return ' + str(highValue) + '; /* #HighValue */\n'
|
||||
body += '}\n'
|
||||
return header, body
|
||||
|
||||
|
||||
def interpolate_lookup_table(table, lowValue):
|
||||
#sortieren, durchgehen und interpolieren.
|
||||
oldX = 0
|
||||
for nextX in sorted(table.keys()):
|
||||
if not oldX in table.keys():
|
||||
if oldX == 0:
|
||||
startY = lowValue
|
||||
nextY = lowValue
|
||||
else:
|
||||
startY = table[oldX]
|
||||
nextY = table[nextX]
|
||||
for thisX in range(oldX, nextX): # die punkte zwischen den stuetzstellen durchgehen
|
||||
thisY = startY + (nextY - startY) * (thisX - oldX) / (nextX - oldX) # lin. interpol.
|
||||
table[thisX] = thisY
|
||||
oldX = nextX
|
||||
return table
|
||||
|
||||
|
||||
def parse_csv(filename):
|
||||
dataType = 'uint16_t'
|
||||
indexType = 'uint8_t'
|
||||
arrayName = 'TestArray'
|
||||
lowestKey = 'ERROR'
|
||||
biggestKey = 'ERROR'
|
||||
highValue = 'ERROR'
|
||||
lowValue = 'ERROR'
|
||||
data = {}
|
||||
csvFile = open(filename, 'r')
|
||||
for line in csvFile:
|
||||
zeile = line.strip().strip('"')
|
||||
if re.match('\#', zeile):
|
||||
myLine = re.split('[\t ;,]+', zeile)
|
||||
if re.match('\#LowValue', myLine[0]):
|
||||
lowValue = int(myLine[1].strip('"'))
|
||||
elif re.match('\#HighValue', myLine[0]):
|
||||
highValue = int(myLine[1].strip('"'))
|
||||
elif re.match('\#DataType', myLine[0]):
|
||||
dataType = str(myLine[1].strip('"'))
|
||||
elif re.match('\#IndexType', myLine[0]):
|
||||
indexType = str(myLine[1].strip('"'))
|
||||
elif re.match('\#ArrayName', myLine[0]):
|
||||
arrayName = str(myLine[1].strip('"'))
|
||||
elif re.match('[0-9]+[,;\t]+[0-9]+', zeile):
|
||||
myLine = re.split('[\t ;,]+', zeile)
|
||||
data[int(myLine[0])] = int(myLine[1])
|
||||
lowestKey = min(data.keys());
|
||||
biggestKey = max(data.keys());
|
||||
return dataType, indexType, arrayName, biggestKey, lowestKey, highValue, lowValue, data
|
||||
|
||||
|
||||
def write_header_file(filename, content, additionalIncludes):
|
||||
headerfile = open(filename, 'w')
|
||||
includeGuard = re.sub('\.', '_', filename) + '_'
|
||||
headerfile.write('#ifndef ' + includeGuard + '\n')
|
||||
headerfile.write('#define ' + includeGuard + '\n\n')
|
||||
for h in additionalIncludes:
|
||||
headerfile.write('#include ' + h + '\n')
|
||||
headerfile.write('\n')
|
||||
headerfile.write(content)
|
||||
headerfile.write('\n\n#endif /* ' + includeGuard + ' */\n\n')
|
||||
headerfile.close()
|
||||
return
|
||||
|
||||
|
||||
def write_body_file(bodyFileName, headerFileName, table, funct):
|
||||
bodyfile = open(bodyFileName, 'w')
|
||||
bodyfile.write('#include "' + headerFileName + '"\n\n')
|
||||
bodyfile.write(table)
|
||||
bodyfile.write('\n\n')
|
||||
bodyfile.write(funct)
|
||||
bodyfile.write('\n\n')
|
||||
bodyfile.close()
|
||||
return
|
||||
|
||||
|
||||
def print_usage():
|
||||
print "MOEP: Benutzung python", sys.argv[0], "bla.csv bla.h bla.c"
|
||||
print "bla.csv muss existieren, bla.h und bla.c werden ueberschrieben"
|
||||
|
||||
|
||||
|
||||
if len(sys.argv) < 4:
|
||||
print_usage()
|
||||
sys.exit()
|
||||
|
||||
|
||||
csvFile = sys.argv[1]
|
||||
headerFileName = sys.argv[2]
|
||||
bodyFileName = sys.argv[3]
|
||||
|
||||
|
||||
dataType, indexType, arrayName, biggestKey, lowestKey, highValue, lowValue, data = parse_csv(csvFile)
|
||||
fullTable = interpolate_lookup_table(data, lowValue)
|
||||
header, body = create_lookup_function(dataType, indexType, arrayName, biggestKey, highValue)
|
||||
|
||||
write_header_file(headerFileName, header, AdditionalIncludes)
|
||||
write_body_file(bodyFileName, headerFileName, create_printable_table(fullTable, dataType, arrayName), body)
|
||||
|
||||
sys.exit()
|
1029
examples/osd/arduino-roller/tools/test2.csv
Normal file
1029
examples/osd/arduino-roller/tools/test2.csv
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue