User Tools

Site Tools


tutorials:arduino:00_arduino_basic:015_freertos

FreeRTOS

Altra manera de fer que es piguin fer dues o mes tasques a la vegada es utilitzar el kernel FreeRTOS, al arduino UNO utilitza el 20%, però es de sobra per fer petits programes i no complicar-se massa la vida quan tinguem, que es el mes normal que fer dues coses a la vegada.

Un exemple de codi basic(sense semafors)

#include <Arduino_FreeRTOS.h>

// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );

// the setup function runs once when you press reset or power the board
void setup() {

  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // Stack size
    ,  NULL
    ,  2  // priority
    ,  NULL );

  xTaskCreate(
    TaskAnalogRead
    ,  (const portCHAR *) "AnalogRead"
    ,  128 // This stack size can be checked & adjusted by reading Highwater
    ,  NULL
    ,  1  // priority
    ,  NULL );

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void TaskAnalogRead(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  for (;;)
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.println(sensorValue);
    vTaskDelay(1);  // one tick delay (15ms) in between reads for stability
  }
}

changing tick frequency

I’m using an atmega328p with FreeRTOS 8.3.2. with 16MHz and 1000 ticks per second. All my delays are like (x / portTICKPERIODMS) where x is the delay in ms. But when I lower the tick value, e.g. to 100 ticks per second, everything runs much faster. I’ve read some topics about tick timing but I’m still not sure what’s wrong here. Can you help me?

Use pdMSTOTICKS(x) instead.

The tick frequency can only be changed at compile time. If you use the pdMSTOTICKS or portTICKPERIODMS macros for all timing then you should be good for tick speeds up to 1KHz

 void TaskBlink02(void *pvParameters){
 pinMode(8,OUTPUT);
 for(;;){
  digitalWrite(8,HIGH);
  vTaskDelay(pdMS_TO_TICKS(1));
  digitalWrite(8,LOW);
  vTaskDelay(pdMS_TO_TICKS(1)); 
 }

Division method is, as predicted, smaller and simpler instruction code as it's division by a constant - the (configTICK_RATE_HZ / 1000) part is computed at compile time. 16000/1000 = 16 (El valor que surt quan faig Serial.println(portTICK_PERIOD_MS); Com ja he descarregat la llibreria compilada el mes raìd es utilitzar el pdMS_TO_TICKS(1); per a un MS

Automats

Una de les possibles aplicacions del Arduino es com PLC, però sempre que intentem fer-ho amb aquest sistema trobem el mateix problema que si pose un delay ja no puc fer altres tasques. A la web de "industrial shields" (Sant Fruitós de Bages), que fan un autòmat industrial basat en arduino he trobat un article que explica el FreeRTOS i sobretot el tema de semàfors. No pose el codi perquè es l'exemple que posa a la mateixa llibreria AnalogRead_DigitalRead.

Semafors

El semàfor més senzill es el MUTEX que utilitza el exemple AnalogRead_DigialRead, que impedeix que el port serie per dues taskes a al vegada.

Amb tecnologia ARM ST32

Amb tecnologia de 32bits, sense IDE, amb GCC i FreeRETOS podem aconseguir un sistema totalment industrial amb CAN Bus, multitasca… i es el que estan utilitzant les noves impressores Marlin 2.0 endavant. beginning_stm32_freertos_linux.pdf

Tot i aquets comentari en que estic totalment d'acord:

There are a number of Windows-based IDE environments available, with varying licenses. But IDEs change, licenses change, and their associated libraries change with time. The advantage of the given IDE is often discarded when the IDE and the operating system it runs upon change. Using a purely open sourced approach has the advantage that you are shielded from all this version churn and burn. You can mothball all of your code and your support tools, knowing that they can all be restored to operation ten years from now, if required. Restoring licensed software, on the other hand, leaves you vulnerable to expired licenses or online sites that have gone dark.

Deixe com de fàcil es progrmar la blue pill amb el IDE d'Arduino i un coversosr TTL que tambè pot fer-se servir una arduino. El problema son els 3,3V. Recordem-ho sempre.getting_started_with_stm32_microcontroller_blinking_of_led_.pdf

Teoria RTOS

Aquest mooc(masive online open course) explica la teoria del RTOS i tambe el freRTOS, el principal problema, està compilat per a windows32. Utilitzen el Visual Estudio… però be la teoria es la mateixa:

1. Capitos 1 i 5, El primer fa una instroducció de que son i perquè serveixen, el 5 acaba explicant una mica la diferencia entre OS normals i RTOS. Son els més interesants. freertos_mooc_1_5.zip

2. Als capitols 2 3 4. Son una més teorics. freertos_mooc_2_3_4.zip

tutorials/arduino/00_arduino_basic/015_freertos.txt · Last modified: 2021/09/19 14:41 (external edit)