From eaf2bd717c037dd6d5d317802c46d9180f1ae967 Mon Sep 17 00:00:00 2001 From: Dan Goldsmith Date: Fri, 8 Mar 2024 08:10:46 +0000 Subject: [PATCH 1/4] Event Queue Starting Code --- Session6/Project_one_EventQueue/.gitignore | 5 ++ Session6/Project_one_EventQueue/.mbedignore | 83 +++++++++++++++++++ .../.vscode/extensions.json | 10 +++ .../Project_one_EventQueue/include/README | 39 +++++++++ Session6/Project_one_EventQueue/lib/README | 46 ++++++++++ .../Project_one_EventQueue/platformio.ini | 22 +++++ Session6/Project_one_EventQueue/src/main.cpp | 31 +++++++ Session6/Project_one_EventQueue/test/README | 11 +++ 8 files changed, 247 insertions(+) create mode 100644 Session6/Project_one_EventQueue/.gitignore create mode 100644 Session6/Project_one_EventQueue/.mbedignore create mode 100644 Session6/Project_one_EventQueue/.vscode/extensions.json create mode 100644 Session6/Project_one_EventQueue/include/README create mode 100644 Session6/Project_one_EventQueue/lib/README create mode 100644 Session6/Project_one_EventQueue/platformio.ini create mode 100644 Session6/Project_one_EventQueue/src/main.cpp create mode 100644 Session6/Project_one_EventQueue/test/README diff --git a/Session6/Project_one_EventQueue/.gitignore b/Session6/Project_one_EventQueue/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Session6/Project_one_EventQueue/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Session6/Project_one_EventQueue/.mbedignore b/Session6/Project_one_EventQueue/.mbedignore new file mode 100644 index 0000000..fe58d10 --- /dev/null +++ b/Session6/Project_one_EventQueue/.mbedignore @@ -0,0 +1,83 @@ +/* Bootloader */ +mbed-os/features/FEATURE_BOOTLOADER/* + +/* BLE */ +mbed-os/connectivity/drivers/ble/* +mbed-os/connectivity/FEATURE_BLE/* + +/* Cellular */ +mbed-os/connectivity/cellular/* +mbed-os/connectivity/drivers/cellular/* +mbed-os/connectivity/netsocket/source/Cellular*.* + +/* Device Key */ +mbed-os/drivers/device_key/* + +/* Experimental */ +mbed-os/platform/FEATURE_EXPERIMENTAL_API/* + +/* FPGA */ +mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/* + +/* Greentea client */ +mbed-os/features/frameworks/greentea-client/* + +/* LORAWAN */ +mbed-os/connectivity/drivers/lora/* +mbed-os/connectivity/lorawan/* + +/* LWIP */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/lwipstack/* + +/* Mbed-client-cli */ +mbed-os/features/frameworks/mbed-client-cli/* + +/* MBED TLS */ +mbed-os/connectivity/drivers/mbedtls/* +mbed-os/connectivity/mbedtls/* + +/* Nanostack */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/nanostack-libservice/* +mbed-os/connectivity/libraries/ppp/* +mbed-os/connectivity/nanostack/* + +/* Netsocket */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/netsocket/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/ppp/* + +/* NFC */ +mbed-os/connectivity/drivers/nfc/* +mbed-os/connectivity/nfc/* + +/* RF */ +/*mbed-os/connectivity/drivers/802.15.4_RF/* */ + +/* Storage */ +mbed-os/storage/filesystem/* +mbed-os/storage/kvstore/* +mbed-os/storage/platform/* + +/* Tests */ +mbed-os/platform/tests/* +mbed-os/TEST_APPS/* +mbed-os/TESTS/* +mbed-os/UNITTESTS/* + +/* Unity */ +mbed-os/features/frameworks/unity/* + +/* Utest */ +mbed-os/features/frameworks/utest/* + +/* USB */ +mbed-os/drivers/usb/source/* +mbed-os/hal/usb/source/* +mbed-os/hal/usb/TARGET_Templates/* + +/* WiFi */ +mbed-os/connectivity/drivers/wifi/* \ No newline at end of file diff --git a/Session6/Project_one_EventQueue/.vscode/extensions.json b/Session6/Project_one_EventQueue/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/Session6/Project_one_EventQueue/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/Session6/Project_one_EventQueue/include/README b/Session6/Project_one_EventQueue/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/Session6/Project_one_EventQueue/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/Session6/Project_one_EventQueue/lib/README b/Session6/Project_one_EventQueue/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/Session6/Project_one_EventQueue/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Session6/Project_one_EventQueue/platformio.ini b/Session6/Project_one_EventQueue/platformio.ini new file mode 100644 index 0000000..3a93d51 --- /dev/null +++ b/Session6/Project_one_EventQueue/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +; --- UNCOMMENT THIS FOR KPIT BOARDS +[env:nucleo_f401re] +platform = ststm32 +board = nucleo_f401re +framework = mbed + +; ----- DANS DEV BOARD +[env:nucleo_f746zg] +platform = ststm32 +board = nucleo_f746zg +framework = mbed + diff --git a/Session6/Project_one_EventQueue/src/main.cpp b/Session6/Project_one_EventQueue/src/main.cpp new file mode 100644 index 0000000..3c7635b --- /dev/null +++ b/Session6/Project_one_EventQueue/src/main.cpp @@ -0,0 +1,31 @@ +#include "mbed.h" + +//Lets have LED blink to show its working +DigitalOut statusLED(LED1); + +// Create a queue that can hold a maximum of 32 events +EventQueue queue(32 * EVENTS_EVENT_SIZE); + + +// Threads +Thread event_thread; //Needs to be global so all elements can post to it + +int main() { + + // put your setup code here, to run once: + + // Start the Event Thread + event_thread.start(callback(&queue, &EventQueue::dispatch_forever)); + + while(1) { + // put your main code here, to run repeatedly: + + + // Blink to let us know things are working + statusLED = !statusLED; + + // Temporary Print Callbak so we we know event queue is working + queue.call(printf, "Tick\n"); + ThisThread::sleep_for(1000ms); + } +} diff --git a/Session6/Project_one_EventQueue/test/README b/Session6/Project_one_EventQueue/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/Session6/Project_one_EventQueue/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html From a5216d17f66ae7ec1dbfd34e763363e500ff70b0 Mon Sep 17 00:00:00 2001 From: Dan Goldsmith Date: Fri, 8 Mar 2024 17:48:49 +0000 Subject: [PATCH 2/4] Project Starting point --- Session6/Project/.gitignore | 5 + Session6/Project/.mbedignore | 83 ++++++++++++++ Session6/Project/.vscode/extensions.json | 10 ++ Session6/Project/include/README | 39 +++++++ Session6/Project/lib/README | 46 ++++++++ Session6/Project/platformio.ini | 22 ++++ Session6/Project/src/main.cpp | 131 +++++++++++++++++++++++ Session6/Project/test/README | 11 ++ 8 files changed, 347 insertions(+) create mode 100644 Session6/Project/.gitignore create mode 100644 Session6/Project/.mbedignore create mode 100644 Session6/Project/.vscode/extensions.json create mode 100644 Session6/Project/include/README create mode 100644 Session6/Project/lib/README create mode 100644 Session6/Project/platformio.ini create mode 100644 Session6/Project/src/main.cpp create mode 100644 Session6/Project/test/README diff --git a/Session6/Project/.gitignore b/Session6/Project/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Session6/Project/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Session6/Project/.mbedignore b/Session6/Project/.mbedignore new file mode 100644 index 0000000..fe58d10 --- /dev/null +++ b/Session6/Project/.mbedignore @@ -0,0 +1,83 @@ +/* Bootloader */ +mbed-os/features/FEATURE_BOOTLOADER/* + +/* BLE */ +mbed-os/connectivity/drivers/ble/* +mbed-os/connectivity/FEATURE_BLE/* + +/* Cellular */ +mbed-os/connectivity/cellular/* +mbed-os/connectivity/drivers/cellular/* +mbed-os/connectivity/netsocket/source/Cellular*.* + +/* Device Key */ +mbed-os/drivers/device_key/* + +/* Experimental */ +mbed-os/platform/FEATURE_EXPERIMENTAL_API/* + +/* FPGA */ +mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/* + +/* Greentea client */ +mbed-os/features/frameworks/greentea-client/* + +/* LORAWAN */ +mbed-os/connectivity/drivers/lora/* +mbed-os/connectivity/lorawan/* + +/* LWIP */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/lwipstack/* + +/* Mbed-client-cli */ +mbed-os/features/frameworks/mbed-client-cli/* + +/* MBED TLS */ +mbed-os/connectivity/drivers/mbedtls/* +mbed-os/connectivity/mbedtls/* + +/* Nanostack */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/nanostack-libservice/* +mbed-os/connectivity/libraries/ppp/* +mbed-os/connectivity/nanostack/* + +/* Netsocket */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/netsocket/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/ppp/* + +/* NFC */ +mbed-os/connectivity/drivers/nfc/* +mbed-os/connectivity/nfc/* + +/* RF */ +/*mbed-os/connectivity/drivers/802.15.4_RF/* */ + +/* Storage */ +mbed-os/storage/filesystem/* +mbed-os/storage/kvstore/* +mbed-os/storage/platform/* + +/* Tests */ +mbed-os/platform/tests/* +mbed-os/TEST_APPS/* +mbed-os/TESTS/* +mbed-os/UNITTESTS/* + +/* Unity */ +mbed-os/features/frameworks/unity/* + +/* Utest */ +mbed-os/features/frameworks/utest/* + +/* USB */ +mbed-os/drivers/usb/source/* +mbed-os/hal/usb/source/* +mbed-os/hal/usb/TARGET_Templates/* + +/* WiFi */ +mbed-os/connectivity/drivers/wifi/* \ No newline at end of file diff --git a/Session6/Project/.vscode/extensions.json b/Session6/Project/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/Session6/Project/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/Session6/Project/include/README b/Session6/Project/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/Session6/Project/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/Session6/Project/lib/README b/Session6/Project/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/Session6/Project/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Session6/Project/platformio.ini b/Session6/Project/platformio.ini new file mode 100644 index 0000000..13274e4 --- /dev/null +++ b/Session6/Project/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +; --- UNCOMMENT THIS FOR KPIT BOARDS +;[env:nucleo_f401re] +;platform = ststm32 +;board = nucleo_f401re +;framework = mbed + +; ----- DANS DEV BOARD +[env:nucleo_f746zg] +platform = ststm32 +board = nucleo_f746zg +framework = mbed + diff --git a/Session6/Project/src/main.cpp b/Session6/Project/src/main.cpp new file mode 100644 index 0000000..c995c11 --- /dev/null +++ b/Session6/Project/src/main.cpp @@ -0,0 +1,131 @@ +#include "mbed.h" + +int MAX_SPEED = 120; + +//Lets have LED blink to show its working +DigitalOut statusLED(LED1); + +// Use Analog 0 to represent speed +AnalogIn speedSensor(A0); +AnalogIn selectSensor(A1); + +// Struct to hold messages +typedef struct{ + int speed; + int user_speed; + float distance; + } sensing_t; + +// Our Message Box +Mail message_queue; + +// For Speed Output +BusOut driver_led(D5,D7,D10); + + +// Create a queue that can hold a maximum of 32 events +EventQueue queue(32 * EVENTS_EVENT_SIZE); + +// Threads +Thread event_thread; //Needs to be global so all elements can post to it +//Thread for sensing +Thread sensing_thread; +// Thread for Conrol +Thread control_thread; + +void driver_led_feedback(int bitmask){ + driver_led = bitmask; +} + + +void handle_sensing(){ + int speed = 0; + int selected_speed = 0; + + while (true){ + //Read from our speed potentiometer Remeber Value here is 0-1 + float speed_pot = speedSensor.read(); + // Convert to speed range + int speed = round(speed_pot * MAX_SPEED); + + float select_pot = selectSensor.read(); + int selected_speed = round(select_pot * MAX_SPEED); + //And Print It for the moment + queue.call(printf, "Current Speed is %d\n", speed); + queue.call(printf, "Selected Speed is %d\n", selected_speed); + + // Ceate a sensor message + if (!message_queue.full()){ + //Block if there is no space + sensing_t *sensor_msg = message_queue.try_alloc_for(Kernel::wait_for_u32_forever); + sensor_msg->speed = speed; + sensor_msg->user_speed = selected_speed; + sensor_msg->distance = 0.0; + + //And Send it + message_queue.put(sensor_msg); + } + + ThisThread::sleep_for(1000ms); + } +} + +void handle_control(){ + int speed_delta; + + while (true){ + // Get the message in blocking mode + queue.call(printf, "Read from Queue\n"); + sensing_t *the_msg = message_queue.try_get_for(Kernel::wait_for_u32_forever); + queue.call(printf, "Read from Queue Complete\n"); + + queue.call(printf, "HANDLE: Speed %d User %d\n", the_msg->speed, the_msg->user_speed); + // Do the processing + speed_delta = the_msg->user_speed - the_msg->speed; + queue.call(printf,"--> Speed Delta is %d\n", speed_delta); + + if (speed_delta > 5){ //Low Speed + queue.call(printf,"--> speed INCREASE needed\n"); + queue.call(driver_led_feedback, 1); //Bitmask 001 (But Endianess) + } + else if (speed_delta < -5){ //High Speed + queue.call(printf,"--> speed DECREASE needed\n"); + queue.call(driver_led_feedback, 4); + } + else { //Speed OK + queue.call(driver_led_feedback, 2); + int foo=0; + } + + message_queue.free(the_msg); //Important, Free space in the queue + // For the moment have a break to avoid thrashig processor + ThisThread::sleep_for(1000ms); + + //Release the queue + + + } +} + + +int main() { + + // put your setup code here, to run once: + + // Start the Event Thread + event_thread.start(callback(&queue, &EventQueue::dispatch_forever)); + sensing_thread.start(handle_sensing); + control_thread.start(handle_control); + + while(1) { + // put your main code here, to run repeatedly: + + + // Blink to let us know things are working + statusLED = !statusLED; + + // Temporary Print Callbak so we we know event queue is working + queue.call(printf, "Tick\n"); + ThisThread::sleep_for(5000ms); + } +} diff --git a/Session6/Project/test/README b/Session6/Project/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/Session6/Project/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html From 15826343e8df11535656b425a993c0116b306f9a Mon Sep 17 00:00:00 2001 From: Dan Goldsmith Date: Sat, 9 Mar 2024 02:55:07 +0000 Subject: [PATCH 3/4] Project --- Session6/Project/mbed_app.json | 8 ++++ Session6/Project/src/main.cpp | 79 +++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 Session6/Project/mbed_app.json diff --git a/Session6/Project/mbed_app.json b/Session6/Project/mbed_app.json new file mode 100644 index 0000000..e070626 --- /dev/null +++ b/Session6/Project/mbed_app.json @@ -0,0 +1,8 @@ +{ + "target_overrides": { + "*": { + "target.printf_lib": "std" + } + } + +} \ No newline at end of file diff --git a/Session6/Project/src/main.cpp b/Session6/Project/src/main.cpp index c995c11..84bc938 100644 --- a/Session6/Project/src/main.cpp +++ b/Session6/Project/src/main.cpp @@ -1,6 +1,7 @@ #include "mbed.h" -int MAX_SPEED = 120; +int MAX_SPEED = 200; +int MAX_DISTANCE = 100; //Lets have LED blink to show its working DigitalOut statusLED(LED1); @@ -8,6 +9,7 @@ DigitalOut statusLED(LED1); // Use Analog 0 to represent speed AnalogIn speedSensor(A0); AnalogIn selectSensor(A1); +AnalogIn distanceSensor(A5); // Struct to hold messages typedef struct{ @@ -33,6 +35,7 @@ Thread sensing_thread; // Thread for Conrol Thread control_thread; + void driver_led_feedback(int bitmask){ driver_led = bitmask; } @@ -54,13 +57,16 @@ void handle_sensing(){ queue.call(printf, "Current Speed is %d\n", speed); queue.call(printf, "Selected Speed is %d\n", selected_speed); + float distance_pot = distanceSensor.read(); + float the_distance = distance_pot * MAX_DISTANCE; + // Ceate a sensor message if (!message_queue.full()){ //Block if there is no space sensing_t *sensor_msg = message_queue.try_alloc_for(Kernel::wait_for_u32_forever); sensor_msg->speed = speed; sensor_msg->user_speed = selected_speed; - sensor_msg->distance = 0.0; + sensor_msg->distance = the_distance; //And Send it message_queue.put(sensor_msg); @@ -70,8 +76,39 @@ void handle_sensing(){ } } +void apply_brakes(){ + /* Fake brake applicaton*/ + + // Get Current Speed (it would be better to get this from elsewhre) + queue.call(printf,"----- EMERGENY BRAKE APPLIED ------\n"); + float speed_pot = speedSensor.read(); + // Convert to speed + int current_speed = speed_pot * MAX_SPEED; + int ledState = 0; // Helper for LEDS + // While we are still moving + while(current_speed > 0){ + //Simulate breaking + current_speed -= 1; + + // Blink LEDS + if (ledState == 0){ + queue.call(driver_led_feedback, 7); + ledState = 1; + } + else { + queue.call(driver_led_feedback, 0); + ledState = 0; + } + + ThisThread::sleep_for(100ms); //Sleep for a Moment + + } +} + void handle_control(){ int speed_delta; + float last_distance = -1; + float distance_delta; while (true){ // Get the message in blocking mode @@ -79,11 +116,24 @@ void handle_control(){ sensing_t *the_msg = message_queue.try_get_for(Kernel::wait_for_u32_forever); queue.call(printf, "Read from Queue Complete\n"); - queue.call(printf, "HANDLE: Speed %d User %d\n", the_msg->speed, the_msg->user_speed); + + queue.call(printf, "HANDLE: Speed %d User %d Distance %0.2f\n", the_msg->speed, the_msg->user_speed, the_msg->distance); + + if(last_distance == -1){ + last_distance= the_msg->distance; + } + // Do the processing speed_delta = the_msg->user_speed - the_msg->speed; queue.call(printf,"--> Speed Delta is %d\n", speed_delta); + // Calculate distance delta + distance_delta = the_msg->distance - last_distance; + // Updte last istance + last_distance = the_msg->distance; + + queue.call(printf,"--> Distance Delta is %f\n", distance_delta); + if (speed_delta > 5){ //Low Speed queue.call(printf,"--> speed INCREASE needed\n"); queue.call(driver_led_feedback, 1); //Bitmask 001 (But Endianess) @@ -94,17 +144,31 @@ void handle_control(){ } else { //Speed OK queue.call(driver_led_feedback, 2); - int foo=0; + } + + // TRivial Call for Distance + if (the_msg->distance < 50){ + //Chek if we are already breaking + queue.call(printf,"--> Contoller Apply Brakes: Start\n"); + // Thread for Brake + Thread braking_thread; + braking_thread.start(apply_brakes); + braking_thread.set_priority(osPriorityHigh); + braking_thread.join(); //Wait for breaking to stop + queue.call(printf,"--> Contoller Apply Brakes: Done\n"); + } message_queue.free(the_msg); //Important, Free space in the queue // For the moment have a break to avoid thrashig processor ThisThread::sleep_for(1000ms); + } - //Release the queue - + //And we are stopped, so Exit + queue.call(printf,"----- Break Success------\n"); + queue.call(driver_led_feedback, 0); - } + //Reset Priority } @@ -114,6 +178,7 @@ int main() { // Start the Event Thread event_thread.start(callback(&queue, &EventQueue::dispatch_forever)); + event_thread.start(callback(&queue, &EventQueue::dispatch_forever)); sensing_thread.start(handle_sensing); control_thread.start(handle_control); From d1e0b743e67d47b7e9aad31bd9027d4ef75b14e4 Mon Sep 17 00:00:00 2001 From: Dan Goldsmith Date: Sat, 9 Mar 2024 02:55:43 +0000 Subject: [PATCH 4/4] Projet files --- .../{Project => Project_Final}/.gitignore | 0 .../{Project => Project_Final}/.mbedignore | 0 .../.vscode/extensions.json | 0 .../{Project => Project_Final}/include/README | 0 .../{Project => Project_Final}/lib/README | 0 .../{Project => Project_Final}/mbed_app.json | 0 .../{Project => Project_Final}/platformio.ini | 0 .../{Project => Project_Final}/src/main.cpp | 0 .../{Project => Project_Final}/test/README | 0 Session6/Project_Two_Feedback/.gitignore | 5 + Session6/Project_Two_Feedback/.mbedignore | 83 +++++++++++ .../.vscode/extensions.json | 10 ++ Session6/Project_Two_Feedback/include/README | 39 ++++++ Session6/Project_Two_Feedback/lib/README | 46 ++++++ Session6/Project_Two_Feedback/platformio.ini | 22 +++ Session6/Project_Two_Feedback/src/main.cpp | 131 ++++++++++++++++++ Session6/Project_Two_Feedback/test/README | 11 ++ 17 files changed, 347 insertions(+) rename Session6/{Project => Project_Final}/.gitignore (100%) rename Session6/{Project => Project_Final}/.mbedignore (100%) rename Session6/{Project => Project_Final}/.vscode/extensions.json (100%) rename Session6/{Project => Project_Final}/include/README (100%) rename Session6/{Project => Project_Final}/lib/README (100%) rename Session6/{Project => Project_Final}/mbed_app.json (100%) rename Session6/{Project => Project_Final}/platformio.ini (100%) rename Session6/{Project => Project_Final}/src/main.cpp (100%) rename Session6/{Project => Project_Final}/test/README (100%) create mode 100644 Session6/Project_Two_Feedback/.gitignore create mode 100644 Session6/Project_Two_Feedback/.mbedignore create mode 100644 Session6/Project_Two_Feedback/.vscode/extensions.json create mode 100644 Session6/Project_Two_Feedback/include/README create mode 100644 Session6/Project_Two_Feedback/lib/README create mode 100644 Session6/Project_Two_Feedback/platformio.ini create mode 100644 Session6/Project_Two_Feedback/src/main.cpp create mode 100644 Session6/Project_Two_Feedback/test/README diff --git a/Session6/Project/.gitignore b/Session6/Project_Final/.gitignore similarity index 100% rename from Session6/Project/.gitignore rename to Session6/Project_Final/.gitignore diff --git a/Session6/Project/.mbedignore b/Session6/Project_Final/.mbedignore similarity index 100% rename from Session6/Project/.mbedignore rename to Session6/Project_Final/.mbedignore diff --git a/Session6/Project/.vscode/extensions.json b/Session6/Project_Final/.vscode/extensions.json similarity index 100% rename from Session6/Project/.vscode/extensions.json rename to Session6/Project_Final/.vscode/extensions.json diff --git a/Session6/Project/include/README b/Session6/Project_Final/include/README similarity index 100% rename from Session6/Project/include/README rename to Session6/Project_Final/include/README diff --git a/Session6/Project/lib/README b/Session6/Project_Final/lib/README similarity index 100% rename from Session6/Project/lib/README rename to Session6/Project_Final/lib/README diff --git a/Session6/Project/mbed_app.json b/Session6/Project_Final/mbed_app.json similarity index 100% rename from Session6/Project/mbed_app.json rename to Session6/Project_Final/mbed_app.json diff --git a/Session6/Project/platformio.ini b/Session6/Project_Final/platformio.ini similarity index 100% rename from Session6/Project/platformio.ini rename to Session6/Project_Final/platformio.ini diff --git a/Session6/Project/src/main.cpp b/Session6/Project_Final/src/main.cpp similarity index 100% rename from Session6/Project/src/main.cpp rename to Session6/Project_Final/src/main.cpp diff --git a/Session6/Project/test/README b/Session6/Project_Final/test/README similarity index 100% rename from Session6/Project/test/README rename to Session6/Project_Final/test/README diff --git a/Session6/Project_Two_Feedback/.gitignore b/Session6/Project_Two_Feedback/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Session6/Project_Two_Feedback/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Session6/Project_Two_Feedback/.mbedignore b/Session6/Project_Two_Feedback/.mbedignore new file mode 100644 index 0000000..fe58d10 --- /dev/null +++ b/Session6/Project_Two_Feedback/.mbedignore @@ -0,0 +1,83 @@ +/* Bootloader */ +mbed-os/features/FEATURE_BOOTLOADER/* + +/* BLE */ +mbed-os/connectivity/drivers/ble/* +mbed-os/connectivity/FEATURE_BLE/* + +/* Cellular */ +mbed-os/connectivity/cellular/* +mbed-os/connectivity/drivers/cellular/* +mbed-os/connectivity/netsocket/source/Cellular*.* + +/* Device Key */ +mbed-os/drivers/device_key/* + +/* Experimental */ +mbed-os/platform/FEATURE_EXPERIMENTAL_API/* + +/* FPGA */ +mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/* + +/* Greentea client */ +mbed-os/features/frameworks/greentea-client/* + +/* LORAWAN */ +mbed-os/connectivity/drivers/lora/* +mbed-os/connectivity/lorawan/* + +/* LWIP */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/lwipstack/* + +/* Mbed-client-cli */ +mbed-os/features/frameworks/mbed-client-cli/* + +/* MBED TLS */ +mbed-os/connectivity/drivers/mbedtls/* +mbed-os/connectivity/mbedtls/* + +/* Nanostack */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/nanostack-libservice/* +mbed-os/connectivity/libraries/ppp/* +mbed-os/connectivity/nanostack/* + +/* Netsocket */ +mbed-os/connectivity/drivers/emac/* +mbed-os/connectivity/netsocket/* +mbed-os/connectivity/libraries/mbed-coap/* +mbed-os/connectivity/libraries/ppp/* + +/* NFC */ +mbed-os/connectivity/drivers/nfc/* +mbed-os/connectivity/nfc/* + +/* RF */ +/*mbed-os/connectivity/drivers/802.15.4_RF/* */ + +/* Storage */ +mbed-os/storage/filesystem/* +mbed-os/storage/kvstore/* +mbed-os/storage/platform/* + +/* Tests */ +mbed-os/platform/tests/* +mbed-os/TEST_APPS/* +mbed-os/TESTS/* +mbed-os/UNITTESTS/* + +/* Unity */ +mbed-os/features/frameworks/unity/* + +/* Utest */ +mbed-os/features/frameworks/utest/* + +/* USB */ +mbed-os/drivers/usb/source/* +mbed-os/hal/usb/source/* +mbed-os/hal/usb/TARGET_Templates/* + +/* WiFi */ +mbed-os/connectivity/drivers/wifi/* \ No newline at end of file diff --git a/Session6/Project_Two_Feedback/.vscode/extensions.json b/Session6/Project_Two_Feedback/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/Session6/Project_Two_Feedback/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/Session6/Project_Two_Feedback/include/README b/Session6/Project_Two_Feedback/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/Session6/Project_Two_Feedback/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/Session6/Project_Two_Feedback/lib/README b/Session6/Project_Two_Feedback/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/Session6/Project_Two_Feedback/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Session6/Project_Two_Feedback/platformio.ini b/Session6/Project_Two_Feedback/platformio.ini new file mode 100644 index 0000000..13274e4 --- /dev/null +++ b/Session6/Project_Two_Feedback/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +; --- UNCOMMENT THIS FOR KPIT BOARDS +;[env:nucleo_f401re] +;platform = ststm32 +;board = nucleo_f401re +;framework = mbed + +; ----- DANS DEV BOARD +[env:nucleo_f746zg] +platform = ststm32 +board = nucleo_f746zg +framework = mbed + diff --git a/Session6/Project_Two_Feedback/src/main.cpp b/Session6/Project_Two_Feedback/src/main.cpp new file mode 100644 index 0000000..c995c11 --- /dev/null +++ b/Session6/Project_Two_Feedback/src/main.cpp @@ -0,0 +1,131 @@ +#include "mbed.h" + +int MAX_SPEED = 120; + +//Lets have LED blink to show its working +DigitalOut statusLED(LED1); + +// Use Analog 0 to represent speed +AnalogIn speedSensor(A0); +AnalogIn selectSensor(A1); + +// Struct to hold messages +typedef struct{ + int speed; + int user_speed; + float distance; + } sensing_t; + +// Our Message Box +Mail message_queue; + +// For Speed Output +BusOut driver_led(D5,D7,D10); + + +// Create a queue that can hold a maximum of 32 events +EventQueue queue(32 * EVENTS_EVENT_SIZE); + +// Threads +Thread event_thread; //Needs to be global so all elements can post to it +//Thread for sensing +Thread sensing_thread; +// Thread for Conrol +Thread control_thread; + +void driver_led_feedback(int bitmask){ + driver_led = bitmask; +} + + +void handle_sensing(){ + int speed = 0; + int selected_speed = 0; + + while (true){ + //Read from our speed potentiometer Remeber Value here is 0-1 + float speed_pot = speedSensor.read(); + // Convert to speed range + int speed = round(speed_pot * MAX_SPEED); + + float select_pot = selectSensor.read(); + int selected_speed = round(select_pot * MAX_SPEED); + //And Print It for the moment + queue.call(printf, "Current Speed is %d\n", speed); + queue.call(printf, "Selected Speed is %d\n", selected_speed); + + // Ceate a sensor message + if (!message_queue.full()){ + //Block if there is no space + sensing_t *sensor_msg = message_queue.try_alloc_for(Kernel::wait_for_u32_forever); + sensor_msg->speed = speed; + sensor_msg->user_speed = selected_speed; + sensor_msg->distance = 0.0; + + //And Send it + message_queue.put(sensor_msg); + } + + ThisThread::sleep_for(1000ms); + } +} + +void handle_control(){ + int speed_delta; + + while (true){ + // Get the message in blocking mode + queue.call(printf, "Read from Queue\n"); + sensing_t *the_msg = message_queue.try_get_for(Kernel::wait_for_u32_forever); + queue.call(printf, "Read from Queue Complete\n"); + + queue.call(printf, "HANDLE: Speed %d User %d\n", the_msg->speed, the_msg->user_speed); + // Do the processing + speed_delta = the_msg->user_speed - the_msg->speed; + queue.call(printf,"--> Speed Delta is %d\n", speed_delta); + + if (speed_delta > 5){ //Low Speed + queue.call(printf,"--> speed INCREASE needed\n"); + queue.call(driver_led_feedback, 1); //Bitmask 001 (But Endianess) + } + else if (speed_delta < -5){ //High Speed + queue.call(printf,"--> speed DECREASE needed\n"); + queue.call(driver_led_feedback, 4); + } + else { //Speed OK + queue.call(driver_led_feedback, 2); + int foo=0; + } + + message_queue.free(the_msg); //Important, Free space in the queue + // For the moment have a break to avoid thrashig processor + ThisThread::sleep_for(1000ms); + + //Release the queue + + + } +} + + +int main() { + + // put your setup code here, to run once: + + // Start the Event Thread + event_thread.start(callback(&queue, &EventQueue::dispatch_forever)); + sensing_thread.start(handle_sensing); + control_thread.start(handle_control); + + while(1) { + // put your main code here, to run repeatedly: + + + // Blink to let us know things are working + statusLED = !statusLED; + + // Temporary Print Callbak so we we know event queue is working + queue.call(printf, "Tick\n"); + ThisThread::sleep_for(5000ms); + } +} diff --git a/Session6/Project_Two_Feedback/test/README b/Session6/Project_Two_Feedback/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/Session6/Project_Two_Feedback/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html