A demonstration of the telemetry library (https://github.com/Overdrivr/Telemetry) to transfert to the desktop various data from the FRDM-TFC shield
Dependencies: BufferedSerial FRDM-TFC-HBRIDGE mbed telemetry
Example of the 'Telemetry' library, a portable communication library for embedded devices.
This code fetches all the data from the Freescale Cup RC-car. It relies on a more advanced shield library that is able to read current feedback from HBridges.
The available data is the following :
- Potentiometer 0
- Potentiometer 1
- 4-bit DIP switch
- Battery level
- current inside HBridge A
- current inside HBridge B
You can use the Pytelemetry Command Line Interface to open plots, visualize the received data, and communicate with the car.
See https://github.com/Overdrivr/pytelemetrycli
main.cpp@2:9f07b14821b0, 2016-02-22 (annotated)
- Committer:
- Overdrivr
- Date:
- Mon Feb 22 21:40:44 2016 +0000
- Revision:
- 2:9f07b14821b0
- Parent:
- 1:eeaf7cbb5582
- Child:
- 3:ddf88c34c61d
Fixed typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Overdrivr | 1:eeaf7cbb5582 | 1 | #include "telemetry/Telemetry.h" |
Overdrivr | 0:4fbaf36176b6 | 2 | #include "FRDM-TFC/TFC.h" |
Overdrivr | 0:4fbaf36176b6 | 3 | |
Overdrivr | 1:eeaf7cbb5582 | 4 | /* |
Overdrivr | 1:eeaf7cbb5582 | 5 | Example of the 'Telemetry' library, a portable communication library for embedded devices. |
Overdrivr | 1:eeaf7cbb5582 | 6 | |
Overdrivr | 1:eeaf7cbb5582 | 7 | This code fetches all the data from the Freescale Cup RC-car. |
Overdrivr | 1:eeaf7cbb5582 | 8 | It relies on a more advanced shield library that is able to read current feedback from HBridges. |
Overdrivr | 1:eeaf7cbb5582 | 9 | |
Overdrivr | 1:eeaf7cbb5582 | 10 | The available data is the following : |
Overdrivr | 1:eeaf7cbb5582 | 11 | * Potentiometer 0 |
Overdrivr | 1:eeaf7cbb5582 | 12 | * Potentiometer 1 |
Overdrivr | 1:eeaf7cbb5582 | 13 | * 4-bit DIP switch |
Overdrivr | 1:eeaf7cbb5582 | 14 | * Battery level |
Overdrivr | 1:eeaf7cbb5582 | 15 | * current inside HBridge A |
Overdrivr | 1:eeaf7cbb5582 | 16 | * current inside HBridge B |
Overdrivr | 1:eeaf7cbb5582 | 17 | |
Overdrivr | 1:eeaf7cbb5582 | 18 | You can use the Pytelemetry Command Line Interface to open plots, visualize the received data, |
Overdrivr | 1:eeaf7cbb5582 | 19 | and communicate with the car. |
Overdrivr | 1:eeaf7cbb5582 | 20 | See https://github.com/Overdrivr/pytelemetrycli |
Overdrivr | 1:eeaf7cbb5582 | 21 | */ |
Overdrivr | 1:eeaf7cbb5582 | 22 | |
Overdrivr | 0:4fbaf36176b6 | 23 | struct TM_state |
Overdrivr | 0:4fbaf36176b6 | 24 | { |
Overdrivr | 0:4fbaf36176b6 | 25 | float throttle; |
Overdrivr | 0:4fbaf36176b6 | 26 | }; |
Overdrivr | 0:4fbaf36176b6 | 27 | |
Overdrivr | 1:eeaf7cbb5582 | 28 | // Definition of the callback function |
Overdrivr | 0:4fbaf36176b6 | 29 | void process(TM_state * state, TM_msg * msg); |
Overdrivr | 0:4fbaf36176b6 | 30 | |
Overdrivr | 0:4fbaf36176b6 | 31 | int main() |
Overdrivr | 0:4fbaf36176b6 | 32 | { |
Overdrivr | 1:eeaf7cbb5582 | 33 | // Init the shield |
Overdrivr | 0:4fbaf36176b6 | 34 | TFC_Init(); |
Overdrivr | 1:eeaf7cbb5582 | 35 | |
Overdrivr | 1:eeaf7cbb5582 | 36 | // Create a Telemetry instance, running on uart at 115200 bauds |
Overdrivr | 1:eeaf7cbb5582 | 37 | Telemetry tm(115200); |
Overdrivr | 0:4fbaf36176b6 | 38 | |
Overdrivr | 1:eeaf7cbb5582 | 39 | // a data structure to hold writeable parameters |
Overdrivr | 1:eeaf7cbb5582 | 40 | // i.e. the car direction and throttle that will be both be controlled from the laptop |
Overdrivr | 1:eeaf7cbb5582 | 41 | TM_state state; |
Overdrivr | 1:eeaf7cbb5582 | 42 | state.throttle = 0.0; |
Overdrivr | 1:eeaf7cbb5582 | 43 | |
Overdrivr | 1:eeaf7cbb5582 | 44 | // Suscribe our custom processing function (= callback function), and pass the data structure |
Overdrivr | 1:eeaf7cbb5582 | 45 | // so that we can access it inside |
Overdrivr | 1:eeaf7cbb5582 | 46 | // This way, everytime a frame is received, Telemetry will call this function for us, |
Overdrivr | 1:eeaf7cbb5582 | 47 | // and pass the TM_state data structure to it. |
Overdrivr | 1:eeaf7cbb5582 | 48 | tm.sub(process, &state); |
Overdrivr | 0:4fbaf36176b6 | 49 | |
Overdrivr | 0:4fbaf36176b6 | 50 | Timer refresh_timer; |
Overdrivr | 0:4fbaf36176b6 | 51 | refresh_timer.start(); |
Overdrivr | 1:eeaf7cbb5582 | 52 | |
Overdrivr | 1:eeaf7cbb5582 | 53 | // Activate the engines ! |
Overdrivr | 0:4fbaf36176b6 | 54 | TFC_HBRIDGE_ENABLE; |
Overdrivr | 1:eeaf7cbb5582 | 55 | |
Overdrivr | 0:4fbaf36176b6 | 56 | for( ; ; ) |
Overdrivr | 0:4fbaf36176b6 | 57 | { |
Overdrivr | 1:eeaf7cbb5582 | 58 | // 20 times per second, re-send data |
Overdrivr | 1:eeaf7cbb5582 | 59 | if(refresh_timer.read_ms() > 50) |
Overdrivr | 0:4fbaf36176b6 | 60 | { |
Overdrivr | 1:eeaf7cbb5582 | 61 | tm.pub_f32("HBcurrentA",TFC_ReadMotorCurrent(0)); |
Overdrivr | 1:eeaf7cbb5582 | 62 | tm.pub_f32("HBcurrentB",TFC_ReadMotorCurrent(1)); |
Overdrivr | 0:4fbaf36176b6 | 63 | tm.pub_f32("pot0",TFC_ReadPot(0)); |
Overdrivr | 0:4fbaf36176b6 | 64 | tm.pub_f32("pot1",TFC_ReadPot(1)); |
Overdrivr | 1:eeaf7cbb5582 | 65 | tm.pub_f32("bat",TFC_ReadBatteryVoltage()); |
Overdrivr | 2:9f07b14821b0 | 66 | tm.pub_u8("dip",TFC_GetDIP_Switch()); |
Overdrivr | 0:4fbaf36176b6 | 67 | tm.pub_f32("throttle",state.throttle); |
Overdrivr | 0:4fbaf36176b6 | 68 | |
Overdrivr | 0:4fbaf36176b6 | 69 | TFC_SetMotorPWM(state.throttle ,state.throttle); |
Overdrivr | 1:eeaf7cbb5582 | 70 | // Update in order to process received data |
Overdrivr | 0:4fbaf36176b6 | 71 | tm.update(); |
Overdrivr | 0:4fbaf36176b6 | 72 | } |
Overdrivr | 0:4fbaf36176b6 | 73 | } |
Overdrivr | 0:4fbaf36176b6 | 74 | } |
Overdrivr | 0:4fbaf36176b6 | 75 | |
Overdrivr | 1:eeaf7cbb5582 | 76 | // This is our processing function called every time a frame is received |
Overdrivr | 1:eeaf7cbb5582 | 77 | // First parameter is a pointer to the data structure we defined in main |
Overdrivr | 1:eeaf7cbb5582 | 78 | // Second parameter is a pointer to a data structure containing all info about received frame |
Overdrivr | 1:eeaf7cbb5582 | 79 | |
Overdrivr | 0:4fbaf36176b6 | 80 | void process(TM_state * state, TM_msg * msg) |
Overdrivr | 0:4fbaf36176b6 | 81 | { |
Overdrivr | 1:eeaf7cbb5582 | 82 | // If data type matches 'float32' |
Overdrivr | 0:4fbaf36176b6 | 83 | if(msg->type == TM_float32) |
Overdrivr | 0:4fbaf36176b6 | 84 | { |
Overdrivr | 0:4fbaf36176b6 | 85 | float th = 0; |
Overdrivr | 1:eeaf7cbb5582 | 86 | // Emplace the data into 'th' |
Overdrivr | 0:4fbaf36176b6 | 87 | if(emplace_f32(msg,&th)) |
Overdrivr | 0:4fbaf36176b6 | 88 | { |
Overdrivr | 1:eeaf7cbb5582 | 89 | // If the emplace has worked, push the value to the actual value state->throttle |
Overdrivr | 1:eeaf7cbb5582 | 90 | // This way the processing is entirely safe |
Overdrivr | 0:4fbaf36176b6 | 91 | state->throttle = th; |
Overdrivr | 0:4fbaf36176b6 | 92 | } |
Overdrivr | 0:4fbaf36176b6 | 93 | } |
Overdrivr | 0:4fbaf36176b6 | 94 | } |