Replies: 17 comments
-
I don't really know how to properly respond to the question, with so little information. I suggest that there is nothing about FreeRTOS that would influence your issue. It is likely to be something to do with the way you're calling your function, or the way the data is being declared, before being returned. To debug the problem, start by trying to flash a LED by calling a function from within the Task. Once that is working, then you're sure your function is being called. You can then move on from there by returning a counter, or something else. Good luck. |
Beta Was this translation helpful? Give feedback.
-
I tried the code below to test it but it is printing #include <Arduino_FreeRTOS.h>
void Blink(void *pvParameters);
void setup() {
Serial.begin(115200);
xTaskCreate(Blink, (const portCHAR *)"blink", 100, NULL, 2, NULL);
}
void loop() {
Serial.println("will loop infinitily");
}
void Blink(void *pvParameters) {
int pin = 13;
pinMode(pin, OUTPUT);
while (true) {
Serial.println("started");
if (isPinHIGH == true) {
digitalWrite(pin, LOW);
delay(1000);
}
else {
digitalWrite(pin, HIGH);
delay(1000);
}
}
}
bool isPinHIGH() {
if (digitalRead(13) == HIGH) {
Serial.println("pin is HIGH");
return true;
}
else {
Serial.println("pin is LOW");
return false;
}
}
|
Beta Was this translation helpful? Give feedback.
-
I'm 90% certain your issue is caused by not allocating enough Task stack. You've only 15 bytes more than the minimum. Hardly enough to store one local string. Using the serial.println() functions requires a lot of local memory. Change the stack size to 500 bytes, and use the stack management tools to check the highwater levels Do not assume delay() will give you what you need. It is a busy wait, and relies on not being interrupted. See issue #20 for more. |
Beta Was this translation helpful? Give feedback.
-
I have removed the #include <Arduino_FreeRTOS.h>
void Blink(void *pvParameters);
void setup() {
Serial.begin(115200);
xTaskCreate(Blink, (const portCHAR *)"blink", 100, NULL, 2, NULL);
}
void loop() {
Serial.println("will loop infinitely");
vTaskDelay(10000); //Any number of ticks you enter it will work fine.
}
void Blink(void *pvParameters) {
int pin = 13;
pinMode(pin, OUTPUT);
while (true) {
Serial.println("started");
if (isPinHIGH == true) {
digitalWrite(pin, LOW);
vTaskDelay(500);
}
else {
digitalWrite(pin, HIGH);
vTaskDelay(500);
}
}
}
bool isPinHIGH() {
if (digitalRead(13) == HIGH) {
Serial.println("pin is HIGH");
return true;
}
else {
Serial.println("pin is LOW");
return false;
}
} |
Beta Was this translation helpful? Give feedback.
-
Try removing the TaskDelay() from the loop(). I noted in the readme that the loop() shall never be blocked, because it is called from the IdleTask. Blocking loop() will kill everything. Then go back to the example, and check it works. Change one thing at a time. See what breaks things. Use the stack high water management tools in FreeRTOS. Start with stack at 500 bytes, and reduce it from there. That, and heap issues, are the cause of most pain. C++ (Arduino) consumes lots of stack. That is the cost of convenience. |
Beta Was this translation helpful? Give feedback.
-
isPinHIGH is a funtion which need () #include <Arduino_FreeRTOS.h>
#define pin 13
void Blink(void *pvParameters);
void setup() {
Serial.begin(115200);
xTaskCreate(Blink, (const portCHAR *)"blink", 100, NULL, 2, NULL);
}
void loop() {
// Serial.println("will loop infinitely");
// vTaskDelay(10000); //Any number of ticks you enter it will work fine.
}
void Blink(void *pvParameters) {
pinMode(pin, OUTPUT);
Serial.println("started");
while (true) {
if (isPinHIGH() == true) {
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin, HIGH);
}
vTaskDelay(500);
}
}
bool isPinHIGH() {
if (digitalRead(pin) == HIGH) {
Serial.println("pin is HIGH");
return true;
}
else {
Serial.println("pin is LOW");
return false;
}
} |
Beta Was this translation helpful? Give feedback.
-
@feilipu if i comment out all lines in the @karawin i am using |
Beta Was this translation helpful? Give feedback.
-
ok but without () it is the address of the function which is compared, not the return of the function. |
Beta Was this translation helpful? Give feedback.
-
Thank you @karawin i didn't noticed that now everything is working well. #include <Arduino_FreeRTOS.h>
#include <TinyGPS++.h>
TinyGPSPlus gps;
void GPSreader(void *pvParameters);
void setup() {
Serial.begin(115200);
Serial2.begin(9600); //GPS module
Serial.println("this is setup func");
xTaskCreate(GPSreader, "GPSreader", 10000, NULL , 2, NULL);
}
void loop() {}
void GPSreader(void *pvParameters) {
Serial.println("something atleast");
while (true) {
String values;
char latitude[10], longitude[10], Altitude[8], Speed[10], satelites[3];
printInt(gps.satellites.value(), gps.satellites.isValid(), 2, satelites);
printFloat(gps.location.lat(), gps.location.isValid(), 9, 6, latitude);
printFloat(gps.location.lng(), gps.location.isValid(), 9, 6, longitude);
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2, Altitude);
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2, Speed);
values = latitude;
values += ',';
values += longitude;
values += ',';
values += Altitude;
values += ',';
values += Speed;
Serial.print("GPS: ");
Serial.println(values); //latitude, longitude, altitude, speed, direction
// vTaskDelay(100);
smartDelay(100);
if (millis() > 2000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}
}
void smartDelay(unsigned long ms) {
unsigned long start = millis();
do {
while (Serial2.available())
gps.encode(Serial2.read());
} while (millis() - start < ms);
}
void printFloat(float val, bool valid, uint8_t len, uint8_t decPnt, char var[]) {
if (!valid) {
while (len-- > 1) {
dtostrf(val, len, decPnt, var);
}
}
else {
dtostrf(val, len, decPnt, var);
}
}
void printInt(uint8_t val, bool valid, uint8_t len, char var[]) {
if (valid) {
sprintf(var, "%d", val);
}
var[len] = 0;
for (int i = strlen(var); i < len; ++i) {
var[i] = '0';
}
if (len > 0) {
var[len - 1] = ' ';
}
} these code works well without FreeRTOS. But now it is not starting the task, I know i used the |
Beta Was this translation helpful? Give feedback.
-
I think that you may use |
Beta Was this translation helpful? Give feedback.
-
You're asking for 10,000 bytes of stack, just for the GPSreader Task alone. If you're trying to run this on an Arduino Uno with 2048 bytes of RAM in total, then you've got a problem. Even a Mega has only 8,192 bytes of RAM. The Again, start with a Task stack of about 500 bytes. Not more, and use the task highwater tools to work out how much stack remains unused. Then reduce to leaving (say) 20 bytes free for each task. Keep the FreeRTOS has To set a one shot or multiple shot Timer, then use the FreeRTOS API for Timers. It is enabled in this port. Oh, and for convenience if you're writing to a serial port, then it is best to protect it using a MutEx Semaphore, otherwise it is possible for one task to interrupt another task writing to the serial port, and you'll garble the messages. And, if you're going to be using a lot of string text messages, wrap them in the |
Beta Was this translation helpful? Give feedback.
-
@feilipu i changed the stack to 500 bytes now it is fine but i am using arduino MEGA with 8Kb of RAM i don't understand why the i get |
Beta Was this translation helpful? Give feedback.
-
Move these lines outside your String values;
char latitude[10], longitude[10], Altitude[8], Speed[10], satelites[3]; Whilst the compiler will probably optimise, and not keep on reallocating them, it is best not tempt fate. And, Finally, calling do {
while (Serial2.available())
gps.encode(Serial2.read());
} while (millis() - start < ms); |
Beta Was this translation helpful? Give feedback.
-
I moved those line above out of the |
Beta Was this translation helpful? Give feedback.
-
I have continued to try to solve that problem but i noted something, the |
Beta Was this translation helpful? Give feedback.
-
strcat, strcpy, sprintf etc, learn a bit more about the possibilities. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for everything now everything is working fine. #include <Arduino_FreeRTOS.h>
#include <timers.h>
#include <TinyGPS++.h>
TinyGPSPlus gps;
void GPSreader(void *pvParameters);
TimerHandle_t GPStimer;
void GPSid() {}
void setup() {
Serial.begin(115200);
Serial2.begin(9600); //GPS module
GPStimer = xTimerCreate("GPStimer", 100 / portTICK_PERIOD_MS, pdFALSE, GPSid, smartDelay);
if (GPStimer == NULL)
Serial.println(F("timer not created"));
else
xTimerStart(GPStimer, 200 / portTICK_PERIOD_MS);
xTaskCreate(GPSreader, "GPSreader", 350, NULL , 2, NULL);
}
void GPSreader(void *pvParameters) {
char* comma = ",";
char values[70];
while (true) {
UBaseType_t uxHighWaterMark;
Serial.println(uxTaskGetStackHighWaterMark(NULL)); //print unused stack memory
char* Latitude = printFloats(gps.location.lat(), gps.location.isValid(), 9, 6);
char* Longitude = printFloats(gps.location.lng(), gps.location.isValid(), 9, 6);
char* Speed = printFloats(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
char* SatNumb = printInts(gps.satellites.value(), gps.satellites.isValid(), 5);
strcpy(values, Latitude);
strcat(values, comma);
strcat(values, Longitude);
strcat(values, comma);
strcat(values, Speed);
Serial.println(values);
vTaskDelay(200 / portTICK_PERIOD_MS);
xTimerReset(GPStimer, 200 / portTICK_PERIOD_MS);
if (xTaskGetTickCount() > 2000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}
}
void smartDelay(unsigned long ms) {
while (Serial2.available())
gps.encode(Serial2.read());
}
/********************************* NEW FOR GPS COORDINATES ******************************/
char* printFloats(float val, bool valid, int len, int decPnt) {
char var[32];
if (!valid) {
while (len-- > 1) {
var[15] = "********";
}
}
else {
dtostrf(val, len, decPnt, var);
}
return var;
}
char* printInts(unsigned long val, bool valid, int len) {
char var[32];
if (valid)
sprintf(var, "%ld", val);
var[len] = 0;
for (int i = strlen(var); i < len; i++)
var[i] = ' ';
if (len > 0)
var[len - 1] = ' ';
return var;
}
void loop() {} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I created a task which reads GPS data but it has to call some function out of the task but when i am running them they are not returning anything.
What could be the cause of that?
Beta Was this translation helpful? Give feedback.
All reactions