MOTION SENSE PAL is used to acquire sensor values.
The ACT of the ACT includes the following.
Sending and receiving wireless packets
Interactive settings mode configuration - <STG_STD>
State transition control by state machine - <SM_SIMPLE>
Uses the motion sensor PAL MOTION SENSE PAL to continuously measure the acceleration of the accelerometer and transmit it wirelessly.
Use the sleep function, to operate on coin cell batteries.
Role | Example |
---|---|
Board BEHAVIOR <PAL_MOT>
is included.
First, register the board BEHAVIOR <PAL_MOT>
. When the board BEHAVIOR is initialized, the sensors and DIOs are initialized. The reason for doing this first is that it is common to check the status of the board's DIP SW, etc., and then configure the network settings, etc.
Here, 3 bits of the 4-bit DIP SW on the board are read and set as the Child Node ID. 0 means no ID (0xFE
).
Set the LED settings. Here the ON/OFF blinking is set to blink every 10ms (in an application that sleeps and has a short wake-up time, this is almost equivalent to setting the LED to turn on during wake-up).
Starts accelerometer measurement. The accelerometer settings (SnsMC3630::Settings
) specify the measurement frequency and measurement range. Here we use 14HZ measurement (SnsMC3630::MODE_LP_14HZ
) with ±4G range (SnsMC3630::RANGE_PLUS_MINUS_4G
).
Once started, the accelerometer takes 14 measurements per second and the values are stored in a FIFO queue inside the sensor. The sensor is notified when 28 measurements have been taken.
The begin()
function exits the setup()
function (after which TWENET is initialized) and is called just before the first loop()
.
Call sleepNow()
after setup()
ends to perform the first sleep.
Before going to sleep, set up an interrupt for the accelerometer's DIO pin, which is generated when the FIFO queue reaches a certain number. The second parameter is PIN_MODE::WAKE_FALLING
. This is a setting to wake up when the pin state changes from HIGH to LOW.
The third line executes sleep with the_twelite.sleep()
. The parameter 60000 is the wake-up setting required to reset the watchdog on the TWELITE PAL board. If not reset, a hard reset will be performed after 60 seconds.
When the accelerometer wakes up from sleep due to a FIFO interrupt from the accelerometer, wakeup()
is called. After that, loop()
is called each time. Before wakeup()
, each peripheral such as UART and devices on the board are woken up (e.g. resetting the watchdog timer). For example, it restarts the LED lighting control.
Here we are initializing variables to be used in loop()
.
Here, the acceleration information stored in the FIFO queue in the accelerometer is retrieved and packet transmission is performed based on this information. After the packet transmission is completed, sleep is executed again.
The b_transmit
variable controls the behavior within loop()
. After a successful transmission request, this value is set to 1, and the program waits for the packet transmission to complete.
First check to see if the sensor is available. Since it is after an interrupted wake-up, it is not normal for it not to be AVAILABLE, and it will go straight to sleep.
It is not used in the wireless transmission packet, but we will check the information on the acceleration taken out.
The measurement results of the accelerometer are stored in a FIFO queue obtained by brd.sns_MC3630.get_que()
.
The structure axis_xyzt
that stores the measurement results of the accelerometer contains the information of three axes (x, y, z) and the sequence number t. The number of samples stored is the size of the queue.
The number of samples stored can be checked by reading the queue size (brd.sns_MC3630.get_que().size()
). Normally 28 samples, but it may go a little further due to processing delays, etc. The first sample is taken from front()'. The first sample can be obtained by
front(). Its sequential number is
front().t`.
Here we will take the average of the samples before taking them out of the queue. Each element of the queue can be accessed with a for statement (for (auto&& v: brd.sns_MC3630.get_que()) { ... }
), where v.x, v.y, v.z
in the for statement is each element. Here, the sum of each element is calculated, and after the for statement, the average is calculated by dividing by the number of elements.
Next, a packet is generated and a request for transmission is made, but because the data volume is large, the packet is divided into two and transmitted twice. Therefore, the sending process is performed twice in the for statement.
The number of samples to be included in the packet to be sent and the sample first sequence number are stored in the first part of the packet payload.
Finally, the acceleration data is stored. While earlier we only referenced each element of the queue to calculate the average value, here we read one sample at a time from the queue and store it in the payload of the packet.
Use .front()
to read the head of the data queue from the accelerometer. After reading, .pop()
is used to release the top of the queue.
The data acquired from the accelerometer is in milli-G units, where 1G is 1000. Since the range is set to ±4G, the data is divided by 2 to be stored within a 12-bit range. To save the number of data, the first 4 bytes store the upper 8 bits of the X, Y and Z axes, and the next 1 byte stores the lower 4 bits of the Z axis, generating a total of 5 bytes.
The transmission ID is stored in the txid[]
array to wait for two transmissions.
Then, if b_transmit
is true
during loop()
, a completion check is performed, and if completed, sleepNow()
puts the program to sleep.
The completion of transmission is confirmed by the_twelite.tx_status.is_complete()
The .txid[]
is the ID value returned on transmission.
Parent Node
MONOSTICK BLUE or RED Act Parent_MONOSTICK to work.
Child Node