Skip to content
Snippets Groups Projects
Commit d55d52db authored by Carlo Alessandro Nicolau's avatar Carlo Alessandro Nicolau
Browse files

Under Development

parent 0e2408fb
No related branches found
No related tags found
No related merge requests found
Showing
with 311 additions and 17 deletions
// Auto-generated template file: command_RESCUE_ENABLE.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_RESCUE_STATUS_GET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_RESCUE_STATUS_RESET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_RESCUE_TIMEOUT_GET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_RESCUE_TIMEOUT_SET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_AVERAGE_GETALL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_AVERAGING_PRESCALER_GET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_AVERAGING_PRESCALER_SET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_GET_SINGLE.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_MAXVALUES_GETALL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_MAXVALUE_RESET.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_OFFSETS_GETALL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SENSOR_VALUES_GETALL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_SWITCH_CONTROL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_USER_PIN_CONTROL.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_VALCHECK.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
// Auto-generated template file: command_VERSION.tpl.c
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#include "../typedefs.h"
......
# some defines from microcontroller firmware
# define COMMUNICATION_PACKET_SOF 0x01 // start of frame value
PACKET_SOF = 0x01
# define COMMUNICATION_PACKET_ADDR 0x30 // address (...yes, only this address is listened to)
PACKET_ADDR = 0x30
# define COMMUNICATION_PACKET_EOF 0x00 // end of frame value
PACKET_EOF = 0x00
def parse_num(_val):
assert isinstance(_val, basestring)
if _val.startswith('0x'):
return int(_val, 16)
else:
return int(_val, 10)
def nibble_to_ascii(_val):
if _val < 10:
r = ord('0') + _val
else:
r = ord('a') + (_val - 10)
return r
def uint8_to_nibbles(_byte):
nibble_0 = nibble_to_ascii(_byte & 0x0f)
nibble_1 = nibble_to_ascii((_byte >> 4) & 0x0f)
return nibble_0, nibble_1
def get_checksum(_rawdata):
checksum = 0
for k in range(1, len(_rawdata)):
checksum = checksum - _rawdata[k]
checksum = checksum % 256
return checksum
def packet_create_rawdata(_command_code, _payload_bytes):
# builds the array of bytes to be sent
rawdata = bytearray()
# start of frame and command code
rawdata.append(PACKET_SOF)
rawdata.append(PACKET_ADDR)
rawdata.append(_command_code)
# append encoded payload data
for val in _payload_bytes:
nibble_0, nibble_1 = uint8_to_nibbles(val)
rawdata.append(nibble_1)
rawdata.append(nibble_0)
# compute the checksum
checksum = get_checksum(rawdata)
rawdata.append(checksum)
# if checksum does NOT equal PACKET_EOF, then add PACKET_EOF
if checksum != PACKET_EOF:
rawdata.append(PACKET_EOF)
return rawdata
def packet_extract_data(_rawdata):
# print _rawdata
assert _rawdata[0] == PACKET_SOF
assert _rawdata[1] == PACKET_ADDR
command_code = _rawdata[2]
# decode payload
payload_bytes = list()
for idx in range(3, len(_rawdata), 2):
if _rawdata[idx] == PACKET_EOF or _rawdata[idx+1] == PACKET_EOF:
break
val = int(str(chr(_rawdata[idx])) + str(chr(_rawdata[idx+1])), 16)
payload_bytes.append(val)
# check checksum
checksum = get_checksum(_rawdata[:-1])
assert _rawdata[idx + 1] == checksum, 'Wrong checksum'
if checksum == PACKET_EOF:
assert (idx + 2) == len(_rawdata)
else:
assert _rawdata[idx + 2] == PACKET_EOF
assert (idx + 2) == len(_rawdata)
return command_code, payload_bytes
#!/usr/bin/python
"""
A minimal utility to continuously read all sensors
"""
import sys
import os
sys.path.insert(0, '../codegen')
import commands
from utils import mls
from analogvariables import *
from bpsentities.descriptors import PayloadFieldEnum, PayloadFieldU8, PayloadFieldU16, PayloadFieldU32
from commutils import *
import serial
from time import time
ASK_CONFIRM = False
SHOW_SENT_DATA = False
SHOW_RECEIVED_DATA = False
UART_DEV = '/dev/ttyUSB1'
def ask_confirm():
answer = ""
while answer not in ["y", "n"]:
answer = raw_input("Confirm [Y/N]? ").lower()
return answer == "y"
if __name__ == '__main__':
# command line options
# example
# python sendcommand.py COMMAND_NAME payload0 payload1 ...
# COMMAND_NAME is the human readable name of the command (e.g.: )
# redirect stdout to null in order to avoid the warnings during object creation
stdout = sys.stdout
f = open(os.devnull, 'w')
sys.stdout = f
switch_list = commands.SwitchList()
analog_variable_list = commands.AnalogVariableList()
digital_variable_list = commands.DigitalVariableList()
user_pin_list = commands.UserPinList()
commands = commands.CommandList(switch_list, analog_variable_list, digital_variable_list, user_pin_list)
# restore normal stdout
sys.stdout = stdout
# open the serial port
ser = serial.Serial(UART_DEV, baudrate=19200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=0.5)
command_name = 'SENSOR_VALUES_GETALL'
filtered_commands = [command for command in commands.entries() if command.name.startswith(command_name)]
command = filtered_commands[0]
# expected number of request fields:
req_field_count = len(command.request_payload)
# prepare the payload
payload_data = list()
payload_field_values = list()
rawdata = packet_create_rawdata(command.request_code, payload_data)
while True:
# send the message over the serial link
ser.write(rawdata)
print "Packet sent."
if SHOW_SENT_DATA:
print "Sent data are:"
for val in rawdata:
print '\t{}\t(ascii: {})'.format(val, chr(val))
# wait for a response (reads up to 1000 bytes, does not parse the packet)
print 'Waiting for response...'
ans = ser.read(1000)
bytes = [ord(c) for c in ans]
print '{} bytes received.'.format(len(ans))
if SHOW_RECEIVED_DATA:
if len(ans):
print 'Received data:'
for k in range(len(ans)):
print '[byte #{}]\t0x{}\t(dec: {}, ascii: {})'.format(str(k), ans[k].encode('hex'), str(ord(ans[k])), ans[k])
if not len(ans):
print 'No response...'
continue
# sys.exit(0)
# parses the response
command_code, payload_bytes = packet_extract_data(bytes)
print "Received raw payolad:", payload_bytes
print "Received packet:"
print ' Command code: {}'.format(command_code)
# print ' Payload data: {} (raw data: {})'.format(payload_field_values, payload_data)
print ' Response payload:'
byte_idx = 0
for field in command.response_payload:
value = -1
if isinstance(field, PayloadFieldU8):
value = payload_bytes[byte_idx]
byte_idx += 1
elif isinstance(field, PayloadFieldU16):
value = payload_bytes[byte_idx] << 8
value += payload_bytes[byte_idx+1]
byte_idx += 2
elif isinstance(field, PayloadFieldU32):
value = payload_bytes[byte_idx] << 24
value += payload_bytes[byte_idx+1] << 16
value += payload_bytes[byte_idx+2] << 8
value += payload_bytes[byte_idx+3]
byte_idx += 4
elif isinstance(field, PayloadFieldEnum):
value = field.get_value_by_index(payload_bytes[byte_idx])
byte_idx += 1
else:
assert False, 'Unmanaged type {}'.format(field.__class__.__name__)
print ' {} = {}'.format(field.name, value)
t = time()
# append read values to a file per sensor, with timestamp
with open('outputs/continuousreads/{}.txt'.format(field.name), 'a') as f:
data = f.write('{}\t{}\n'.format(t, value))
#!/usr/bin/python
"""
Generates a list of the available BPS commands
"""
import sys
import os
sys.path.insert(0, '../codegen')
import commands
from entities.descriptors import PayloadFieldEnum
from utils import mls
def print_field_description(_field, _field_idx):
print ' FIELD #{} ({}) : {}, {}'.format(mls(str(_field_idx), 2), mls(_field.ctype, 8), _field.name, _field.description)
if isinstance(_field, PayloadFieldEnum):
print ' {} indexing:'.format(_field.name)
for index in _field.index_list:
print ' [{}] --> {}'.format(index, _field.get_value_by_index(index))
if __name__ == '__main__':
# redirect stdout to null in order to avoid the warnings during object creation
stdout = sys.stdout
f = open(os.devnull, 'w')
sys.stdout = f
switch_list = commands.SwitchList()
# analog_variable_list = commands.AnalogVariableList()
# digital_variable_list = commands.DigitalVariableList()
analog_variable_list = commands.AnalogVariableList(
_var_enum_index_start=1,
_alarm_enum_index_start=1)
digital_variable_list = commands.DigitalVariableList(
_var_enum_index_start=max(analog_variable_list.get_var_enum_indexes())+1,
_alarm_enum_index_start=max(analog_variable_list.get_alarm_enum_indexes())+1)
user_pin_list = commands.UserPinList()
commands = commands.CommandList(switch_list, analog_variable_list, digital_variable_list, user_pin_list)
# restore normal stdout
sys.stdout = stdout
if len(sys.argv) == 1:
print "List of available commands:"
for command in commands.entries():
print ' ' + mls(command.name, 32), command.description
print 'Type help.py COMMAND to obtain a detailed description, or help.py ALL to show details for all commands.'
elif len(sys.argv) == 2:
command_name = sys.argv[1].upper()
if command_name == 'ALL':
filtered_commands = commands.entries()
else:
filtered_commands = [command for command in commands.entries() if command.name.startswith(command_name)]
print 'Details of commands starting with "{}":'.format(command_name)
if not filtered_commands:
print 'List is empty.'
for command in filtered_commands:
print ''
print ' COMMAND NAME : "{}"'.format(command.name)
print ' DESCRIPTION : {}'.format(command.description)
print ' REQUEST CODE : {}'.format(command.request_code)
print ' RESPONSE CODE : {}'.format(command.response_code)
print ' REQUEST PAYLOAD :'
print ' LENGTH : {} fields, {} bytes'.format(len(command.request_payload), command.get_request_payload_len())
field_idx = 0
for field in command.request_payload:
print_field_description(field, field_idx)
field_idx += 1
print ' RESPONSE PAYLOAD :'
print ' LENGTH : {} fields, {} bytes'.format(len(command.response_payload), command.get_response_payload_len())
field_idx = 0
for field in command.response_payload:
print_field_description(field, field_idx)
field_idx += 1
print ''
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment