View previous topic :: View next topic |
Author |
Message |
red_one
Joined: 30 Nov 2007 Posts: 10
|
rtos_msg_read and rtos_msg_send : used with a float number |
Posted: Wed Jan 02, 2008 9:02 am |
|
|
I have two task with message send and read.
Code: |
#include <16f877A.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock = 10000000)
#use rs232 (baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
/* this tells the compiler that the rtos functionality will be needed, that
timer0 will be used as the timing device, and that the minor cycle for
all tasks will be 20 miliseconds */
#use rtos(timer = 0, minor_cycle = 20ms)
#include <ds1631.c>
unsigned int32 tt;
float current_mean_temperature;
#task(rate = 1000ms, max = 20ms, queue = 1)
void mean_temperature_task();
#task(rate = 1000ms, max = 20ms, queue = 1)
void read_mean_temperature_task();
void mean_temperature_task() {
unsigned int16 new_temperature;
new_temperature = (unsigned int16) read_full_temp();
delay_ms(1000);
if (tt > 0) {
current_mean_temperature *= tt;
current_mean_temperature += new_temperature;
current_mean_temperature /= tt + 1;
}
else
current_mean_temperature = new_temperature;
rtos_msg_send(read_mean_temperature_task, current_mean_temperature);
if (tt == 0xFFFFFFFF)
tt = 1;
else
tt++;
}
void read_mean_temperature_task() {
float mean;
if(rtos_msg_poll() > 0)
mean = rtos_msg_read();
printf("\r\nt=%ld\trtos_msg_read : mean temperature=%g", tt, mean);
printf("\r\nt=%ld\tglobal var : mean temperature=%g", tt, current_mean_temperature);
}
void main() {
init_temp();
tt = 0;
rtos_run();
}
|
what I get in serial I/O monitor
t=0 rtos_msg_read : mean temperature=139.00
t=0 global var : mean temperature=6990.46
t=1 rtos_msg_read : mean temperature=139.00
t=1 global var : mean temperature=6990.00
t=2 rtos_msg_read : mean temperature=139.00
t=2 global var : mean temperature=6993.00
t=3 rtos_msg_read : mean temperature=139.00
t=3 global var : mean temperature=6994.00
t=4 rtos_msg_read : mean temperature=139.00
t=4 global var : mean temperature=6994.50
t=5 rtos_msg_read : mean temperature=139.00
t=5 global var : mean temperature=6994.80
t=6 rtos_msg_read : mean temperature=139.00
t=6 global var : mean temperature=6995.00
t=7 rtos_msg_read : mean temperature=139.00
t=7 global var : mean temperature=6995.14
why values are not the same ? |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Wed Jan 02, 2008 10:14 am |
|
|
It appears as though your tasks are running at the same time so when you print current_mean_temperature in the read function you can't be sure where the calculation is in your mean_temperature_task function. I would put the printing of the current_mean_temperature in the mean_temperature_task function and see how the numbers stack up then. |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Wed Jan 02, 2008 10:16 am |
|
|
Check out the help comments on the rtos_msg_send and read. It looks like it only sends a byte at a time. your value is probably being truncated. |
|
|
Guest
|
|
Posted: Wed Jan 02, 2008 12:04 pm |
|
|
Heath wrote: | Check out the help comments on the rtos_msg_send and read. It looks like it only sends a byte at a time. your value is probably being truncated. |
Yes, you're right. Even with an unsigned int16 variable it does'nt work. So it's impossible to send float number as message between tasks. |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Wed Jan 02, 2008 1:14 pm |
|
|
If you had a int16 couldn't you reserve like 5 bytes and just put the bytes together as they come in?
It seems doable to me. You'd probably have to work out a protocol so you know the start of a number then the next 2 bytes would be my value. |
|
|
|