Slave JSON Files - SpartanHyperloop3/main GitHub Wiki
Outputs
The outputs JSON file contains control information that is used to activate outputs and functions corresponding to the current state.
Ex 1. Outputs are set corresponding to state 1
{
"1" : {
"function" : [{
"name" : "printState",
"arguments" : [1,2,3],
"threaded" : 0
},
{
"name" : "PWMtest",
"arguments" : [],
"threaded" : 1
}],
"GPIO" :
[
[18, 1],
[8, 0],
[24, 0]
],
"PWM" :
[
[14, 55, 10],
[12, 130, 50]
]
}
}
The initial key is the current state and is used by the state machine to find the corresponding outputs that should be activated. The value is a dictionary that contains keys that denote the type of output. Valid output types are function, GPIO, and PWM.
The function output contains a list of dictionaries corresponding to a function that should be activated upon entering the state. Each dictionary should contain 3 keys:
Key | Use |
---|---|
name | The name of the function in code |
arguments | The arguments to be passed to the function |
threaded | If the function should be ran in its own thread |
The GPIO output contains a list of two element lists which denote a GPIO and the corresponding state to set the GPIO to. The first element is the number corresponding to the GPIO output in BCM notation. eg. [18, 1]
will set GPIO 18 to high. Its is important that the desired output for each GPIO is set for each state.
The PWM output contains a list of three element lists which denote a GPIO that is to be used for as PWM signal. The first element in the list is the GPIO output number in BCM notation. The second element is the frequency of the PWM signal, and the third elements is the duty cycle. eg. [14, 55, 10] sets pin 14 to a frequency of 55 Hz with a 10% duty cycle. The PWM signals are initiated at startup with an initial duty cycle of 0 Hz.
Sensors
The sensors JSON file is used to initiate threads for sensor readings. Different threads should be used for different sampling rates and data which comes from the same sensor and should be concurrent should occur in the same thread. All readings are sent to the master with the corresponding sensor name. The sensor data is also stored in a local global storage for possible use in post-processing. The master must be aware of the sensor names being sent to it, meaning they should exist is the master's input state logic JSON file.
Ex 1. A thread is created for a sensor reading on the I2C bus. This thread contains no initialization data.
{
"thread1" : [
{
"bus" : "i2c",
"sample_rate" : 0.01
},
{
"PI_1_U1_CH1" : {
"location" : 105,
"write" : [],
"read" : [16],
"data_length" : "block",
"data_type" : "pos",
"operations" : "R",
"data_processing_callback" : {
"function" : "ADCtoVolts12Bit",
"args" : []
}
}
}
]
}
The first key is the name of the thread, this information can be arbitrary and is mainly used for debugging. The thread name contains a list with two elements. The first element contains a dictionary with information on the bus from which the data is retrieved, the sampling rate, and initialization data to be sent before entering an indefinite loop. At this time only the I2C bus is supported. The second element contains a dictionary with keys corresponding to the sensor name to be sent to the master. Each key contains a dictionary with information necessary for communicating with the sensor. The keys and their function are as follows:
Key | Function |
---|---|
location | contains the address of the sensor in decimal |
write | contains a list of bytes in decimal to send to the sensor, occurs before a read operation in RW |
read | contains a register in decimal to read from, should be enclosed in brackets |
data_length | byte will read a byte, word will read two bytes, block will read 32 bytes as a list of bytes |
operations | the type of operation can be: R to read from the sensor, W to write to the sensor, RW to first write and then read from the sensor |
data_processing_callback | A function to be called and passed the reading for data conversion or post processing. The function should return the processed data so it can be sent to the master after the function is complete. The "function" key should be the function name in code, and the "args" key should be any additional arguments needed to be passed to the function. In code the reading should always be the first argument in the functions definition. If no additional arguments are need an empty list should be used. |
Ex 2. A thread is created for multiple concurrent readings on the I2C bus and contains initialization data.
{
"thread6" : [
{
"bus" : "i2c",
"sample_rate" : 0.1,
"init" : [
{
"location" : 104,
"register" : 107,
"data" : 0
},
{
"location" : 104,
"register" : 28,
"data" : 0
}
]
},
{
"PI_1_Accelerometer_X" : {
"location" : 104,
"write" : [],
"read" : [59],
"data_length" : "word",
"operations" : "R",
"data_type" : "2c",
"data_processing_callback" : {
"function" : "convertByLSB",
"args" : [16384.0]
}
},
"PI_1_Accelerometer_Y" : {
"location" : 104,
"write" : [],
"read" : [61],
"data_length" : "word",
"operations" : "R",
"data_type" : "2c",
"data_processing_callback" : {
"function" : "convertByLSB",
"args" : [16384.0]
}
},
"PI_1_Accelerometer_Z" : {
"location" : 104,
"write" : [],
"read" : [63],
"data_length" : "word",
"operations" : "R",
"data_type" : "2c",
"data_processing_callback" : {
"function" : "convertByLSB",
"args" : [16384.0]
}
}
}
]
}
Initialization data is denoted by the "init"
key in the first element of the thread's list. the "init"
key contains dictionaries with three keys for sending single initialization bytes to sensors. Initialization data keys are as follows:
Key | Value |
---|---|
location | the address of the sensor in decimal |
register | the register to write to in decimal |
data | the data to write to the register in decimal |
When multiple sensor readings are put into one the thread the order in which they are written in the JSON file is the order in which they will be read from the bus. Eg. in the example above "PI_1_Accelerometer_X"
will occur before "PI_1_Accelerometer_y"
.
Buttons
The buttons JSON file contains the GPIOs which will be used as digital switches. GPIO numbers are in BCM notation.
Ex 1. A digital input is initiated at GPIO 7 with the internal pull up resistor activated.
{
"Emergency_Override" : {
"PUD" : "PUD_UP",
"location" : 7
}
}
"PUD"
can be either "PUD_UP"
or "PUD_DOWN"
.