Site icon Elektrologi

Percobaan Penjadwal Periodik FreeRTOS Dengan 2 Task

Berikut ini percobaan 2 task periodik pada FreeRTOS

Spesifikasi

Task1:

Task2:

Perangkat Lunak

// 1 task periodik dengan FreeRTOS Arduino Nano (ATmega328)
// template umum FreeRTOS di Arduino Nano  https://github.com/feilipu/Arduino_FreeRTOS_Library/blob/master/examples/AnalogRead_DigitalRead/AnalogRead_DigitalRead.ino
// contoh pemakaian vTaskDelayUntil dan xTaskGetTickCount dari https://github.com/feilipu/avrfreertos/blob/master/MegaBlink/main.c

#include "Arduino_FreeRTOS.h"
#include <semphr.h>

#define LED1  2
#define LED2  3
#define LED3  4
#define LED4  5

static void TaskBlinkLED1(void *pvParameters);
static void TaskBlinkLED2(void *pvParameters);

// task handle diperlukan jika ingin mengakses parameter suatu task. optional
TaskHandle_t xHandle1 = NULL;

void TaskDummy(int LED_A, int LED_B, long int counter) {
  long int i;
  for (i = 0; i < counter; i++) {
    cli();
    digitalWrite(LED_A, LOW);
    digitalWrite(LED_B, HIGH);
    sei();
    asm("nop");
    cli();
    digitalWrite(LED_A, HIGH);
    digitalWrite(LED_B, LOW);
    sei();
    asm("nop");
  }
  digitalWrite(LED_A, LOW);  // matikan semua LED
  digitalWrite(LED_B, LOW);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  xTaskCreate(
    TaskBlinkLED1
    ,  "Task1"  // A name just for humans
    ,  100  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  &xHandle1 );
xTaskCreate(
    TaskBlinkLED2
    ,  "Task2"  // A name just for humans
    ,  100  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  3  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  &xHandle1 );    
}

void loop() {
}

static void TaskBlinkLED1(void *pvParameters) // Main Red LED Flash
{
  TickType_t xLastWakeTime;
  /* The xLastWakeTime variable needs to be initialised with the current tick
    count.  Note that this is the only time we access this variable.  From this
    point on xLastWakeTime is managed automatically by the vTaskDelayUntil()
    API function. */
  xLastWakeTime = xTaskGetTickCount();
  while (1)
  {
    Serial.println(millis());
    Serial.println(xLastWakeTime); // cetak last wake time, untuk debugging. hanya untuk slow system

    TaskDummy(LED1, LED2, 10000L / 0.172 * 4 ); // 4 detik waktu eksekusi
    vTaskDelayUntil( &xLastWakeTime, ( 10000L / portTICK_PERIOD_MS ) ); // 10 detik perioda

  }
}

static void TaskBlinkLED2(void *pvParameters) // Main Red LED Flash
{
  TickType_t xLastWakeTime;
  /* The xLastWakeTime variable needs to be initialised with the current tick
    count.  Note that this is the only time we access this variable.  From this
    point on xLastWakeTime is managed automatically by the vTaskDelayUntil()
    API function. */
  xLastWakeTime = xTaskGetTickCount();
  while (1)
  {
    Serial.println(millis());
    Serial.println(xLastWakeTime); // cetak last wake time, untuk debugging. hanya untuk slow system

    TaskDummy(LED3, LED4, 10000L / 0.172 ); // 1 detik waktu eksekusi
    vTaskDelayUntil( &xLastWakeTime, ( 2000L / portTICK_PERIOD_MS ) ); // 2 detik perioda

  }
}

 

Pengukuran

Berikut ini adalah tampilan kedua buah task di layar osiloskop.

Sinyal warna kuning adalah Task 1

Sinyal warna biru adalah Task 2.

 

Pengukuran pewaktuan dengan osiloskop

Referensi

Exit mobile version