ANSI C Ringspeicher..Codes?etc
-
Hallo Zusammen,
Ich muß eine Ringspeicher für µ.C Progrimmieren,da Voraussetzung sind z.B "Messwerte sind für jeden Kanal in einem Ringspeicher(Circular_Buffer) zu speichern,Die Größe des Speichers ist wählbar, muss aber getestet sein was maximal möglich ist. Es muss wählbar sein welche Mittelwerte (1, 5, 15, 30, 60 Minutenmittelwerte) gespeichert werden. Es muss eine Option geben den Ringspeicher / Log File auf einen Rechner zu übertragen. Im Fall von Alarmauslösung ist der Ringspeicher voll zu schreiben, aber nicht mehr zu überschreiben. Bei Alarmauslösung werden automatisch 1 Minutenmittelwerte gespeichert. Eine entsprechend sinnvolle Quitierung und Löschung ist zu berücksichtigen.
Wenn eine hat Idea,dann bitte teilen.Ein einfach Ringspeicher (FIFO) habe ich gebildet,aber Voraussatzung wegen Mittelwerte z.B 1, 5, 15, 30, 60 Minuten und noch Sache von Alarmauslösung bekomme ich unter Luft.Falls eine hat Codes in ANSI oder eine Idea dann bitte Helfen.
Ich danke im Voraus,
Slfzn
MfG
-
Bitte verwende vernünftiges Deutsch.
Ich tippe mal: Erstsemester, Muttersprache != Deutsch, kommt mit Aufgabenstellung seines Lehrers nicht klar und versuchte, mit irgendeinem Tool seine Frage zu übersetzen.
Schreibe zu deiner Aufgabenstellung deinen compilierbaren Quellcode hin, deine kompletten Hausaufgaben macht dir hier kaum jemand.
Also sei nicht so faul.
-
Wutz schrieb:
Bitte verwende vernünftiges Deutsch.
Ich tippe mal: Erstsemester, Muttersprache != Deutsch, kommt mit Aufgabenstellung seines Lehrers nicht klar und versuchte, mit irgendeinem Tool seine Frage zu übersetzen.
Schreibe zu deiner Aufgabenstellung deinen compilierbaren Quellcode hin, deine kompletten Hausaufgaben macht dir hier kaum jemand.
Also sei nicht so faul.Hi Ich weiß nicht was du reklamieren willst ? deutsche sprache ist hier nicht zu tun.Da die Frage Schilderungen ist sehr einfach.Ich will nicht jemend meine Hausaufgaben macht,das war eigentlich keine Hausaufgaben!
....ganz einfach eine Projekt mache ich gerade...ja klar bin ich Anfänger
Ich kann auch auf Englisch schreiben " I have to Program Circular Buffer,this circular buffer will compute the measurments values which will received by two sensors let say! temprature and pressure and then my program which i write in ANSI C will calculate the arithmetic average of measurments over a period of 1 or 5 or 15 or 30 or 60 minutes The user could chose the period on configuration screens etc,i develope C codes for circular buffer like this very easy and simple for every one to understand.
#############################################################################
/* CIRCULAR BUFFER QUEUE */
#include <stdio.h>
#include <stdlib.h>#define BUFFER_SIZE 8
int data_size = 0; // number of chars in buffer
int read_pointer = 0; // indice number of last read char
int write_pointer = 0; // indice number of last written char
int input; // user input
char add; // char to addchar buffer[BUFFER_SIZE];
// prototypes
int buffer_full(void);
int buffer_empty(void);
void push_char(char c);
void pull_char(void);int main(void)
{int i;
printf("Circular Buffer Queue Implementation");
// make sure there are no random chars in array, all spaces
for (i = 0; i < BUFFER_SIZE; i++) buffer[i] = 0x20;while (input != 4) {
printf("\n press 1 to push char");
printf("\n press 2 to pop char");
printf("\n press 3 to show queue");
printf("\n press 4 to exit\n");
scanf("%d", &input);// push char
if (input == 1) {printf("\nEnter char: ");
scanf("%c", &add);
scanf("%c", &add); // twice otherwise it will get the last enter as inputif (! buffer_full())
push_char(add);
else
printf("\nBUFFER IS FULL!");}
// pull char
else if (input == 2) {if (! buffer_empty())
pull_char();
else
printf("\nBUFFER IS EMPTY!");
}
// display buffer info
else if (input == 3) {printf("\n data_size: %d read_pointer: %d write_counter: %d",
data_size, read_pointer, write_pointer);printf("\nQueue content:\n");
for (i = 0; i < BUFFER_SIZE; i++) printf("[%c]", buffer[i]);}
printf("\n----");
}return 0;
}// adds a char
void push_char(char c)
{
// increase write_pointer, check if at end of array
if (++write_pointer >= BUFFER_SIZE) write_pointer = 0;buffer[write_pointer] = c;
data_size++;
}// returns 1 if buffer is full, 0 if buffer is not full
int buffer_full(void)
{
return read_pointer == write_pointer &&
data_size == BUFFER_SIZE;
}
// returns 1 if buffer is empty, 0 if buffer is not empty
int buffer_empty(void)
{
return read_pointer == write_pointer &&
data_size == 0;
}// pull char from queue
void pull_char(void)
{
if (++read_pointer >= BUFFER_SIZE) read_pointer = 0;printf("\nPopped char %c", buffer[read_pointer]);
// enter space on place of read char so we can see it is removed
buffer[read_pointer] = 0x20;
data_size--;
}
###########################################################################But now i want to include codes about arithmetic average of measurments values over periode of 1,5,15,30 or 60 minutes or i want to add requeirment of arithmetic average of measurments over periode of 1,5,15,30 or 60 minutes
Ich hoffe jetzt schon klar oder auch
immer Nicht_vernünftiges_Deutsch
mach probleme.....any how..danke für Antowrt.
Viele Grüße
Slfz
-
Wenn du dich mit Englisch wohler fühlst, dann bitte, schreib auf Englisch. Wir können auch gerne auf Englisch antworten. Auf jeden Fall sollte was du schreibst verständlich sein und das war dein erster Beitrag nicht.
Zu deiner Frage: Was ist deine Frage? Du hast all dies geschrieben, du musst doch einen Plan haben, wie es weitergehen soll. Eine Stelle an der du nicht weiterkommst. Diese Stelle kann auch ein fehlender Ansatz sein, dafür müsstest du aber die Aufgabenstellung deutlich klarer formulieren. Was meinst du hier mit den Zeiten? Echte Zeit? Haben die Daten einen Zeitstempel? Soll der Wert kontinuierlich aktualisiert werden oder nur in ausgewählten Intervallen? Ich könnte noch mehrere Seiten solcher Fragen stellen, dermaßen unklar ist, was du überhaupt wissen möchtest.
Lies dir vielleicht mal dies durch:
http://www.c-plusplus.net/forum/136013
http://www.c-plusplus.net/forum/200753
http://www.catb.org/~esr/faqs/smart-questions.html (deutsche Übersetzung über die beiden obigen Links findbar)
Da lernst du, bessere Fragen zu stellen, die bessere Antworten ergeben.Und noch ein letzter Hinweis: Bitte formatiere Code, indem du ihn mit den cpp-Tags einschließt. Auch zu finden unter dem Editierfenster für Beiträge, unter den Smileys, ganz links. Unformatierten Quelltext wird sich niemand durchlesen, besonders nicht in dieser Menge. Die Tags kannst du auch noch nachträglich in deinen vorherigen Beitrag reinediteren, dann wirkt der Thread auf neue Leser nicht so abschreckend*. Bei mehreren Seiten unformatiertem Quelltext schalten viele mögliche Helfer nämlich sofort ab.
*: Im Prinzip könnte ich das auch für dich übernehmen, aber so sehe ich, ob du diese Antworten überhaupt liest und ernst nimmst.
-
Hallo SeppJ ,
Zuerst Vielen dank für Antwort.Ok kann ich in Englisch noch einfacher schreiben.
I have to program circular buffer/Ringspeicher in ANSI C, this Circular buffer program should calculate average of measurment values in selected time interval (ausgewählten Intervallen) those interval could be 1 or 5 or 15 or 30 or 60 minutes.
That's mean if user want,then he/she can select any time sample which i mention above to find the average of measurments values.
I wrote program for circular buffer,i can add and delete elements from buffer,and can also see how many element are in buffer,but i want to know how can i write program which calculate average values of measurments in selectable time interval (real-time)/Echte Zeit.
I hope now it's clear if not let me know i will clear in more easy way.
V.G
Slfzn
-
Depends: Do you need the averages every X minutes or do you need them, when the user asks for them. More specifically:
Scenario A:
Once every X minutes the averages for the last 1 or 5 or 15 or 30 or 60 minutes are queried.Scenario B:
The user queries at an unknown time and wants to know the current averages for the last 1 or 5 or 15 or 30 or 60 minutes.Scenario A has a simple solution: 1 or 5 or 15 or 30 or 60 minutes before the query you start measuring. And that's it.
Scenario B is complicated. You have to deal with the case that there was more data in the last 60 minutes than there is space in your buffer. The only way I can think of right now is not using a ring buffer :p but a queue instead. Every date in the queue has a timestamp and is only discarded when it is older than 60 minutes. When the user queries, it is trivial to calculate the averages. But using a queue or any other dynamic structure (others would work too, but I cannot think of a static solution for this scenario) defeats the whole purpose of using the buffer (Am I correct that the static buffer is used because of hardware limitations?).
-
Well,first of all thanks as you got it! what i want to ask and understand my question.
Now your question i have to answer which you asked in your last thread very last part,"(Am I correct that the static buffer is used because of hardware limitations?)"
Little Bit About Hardware & System:
------------------------------------
Yes Buffer is static and i can clear in more details whole system and integration of Soft & hardware,actually i have one Microcontroller (AT39) Type 32bit maschine of company name GRAF-SYTECO for more details (http://www.graf-syteco.de/html/englisch/at_3_series.html),now this AT39 µ.C have also small LCD screen,on right hand side of screen there are four small buttons those we use to operate AT39 µ.C.Now this AT39 can be integrate with 5 hub connection at bottom,these hub connection can be use for sensors to connect AT39 with any physical system(out side) e.g emergency cooling system of fresh water or any gas etc etc..und und und,these sensors can take reading of water or gas, let say parameters are temprature and pressure etc.
On my PC or MAC etc i have IDE which help me to operate AT39 via RS232,this IDE also have C compiler for ANSI standard,so i can program AT39 in my C codes to operate AT39 with total control and can moniter any system (as i mnetion above water colling system etc) effectively.So with help of given button on AT39 user can also select any values like i asked time(in minutes i.e 1 min or 5 min or 15 min.....60min) etc to calculate average values of measurments e.g temp or pressure) and my program should give average of measurements values either 1 min or 5 min or 15 min or 30 min or 60 min,that's mean user should have choice to select any time sample which i mention above and get average measurments values.
I feel what you mention in Scenario A: "Once every X minutes the averages for the last 1 or 5 or 15 or 30 or 60 minutes are queried" looks exactly what i need to program.As i already know time samples(1,5,15,30 0r 60 etc), so Scenario B for unknow time not fit for my application.
Any way,if you have any idea about Scenario A: then share,i mean how to code etc?,or either you know any examples or name of book for such situation,will be great
...i case you feel bothered then just ignore,i know all this is bit fuzzy
...but now i like it all to do.
Regards
Slfzn
-
For Scenario A you do not need your buffer at all. You have several (static) list of running averages, one for each time interval. Each average in these lists is some struct with a running sum and the number of values that were added. Each time you get a new number, you add it to all these averages.
Imagine you query the averages once every minute. Then you need one 1-minute-average. This average is reset after each query. You need a list of five 5-minute-averages. At each query the oldest average is shown and then reset. It would be a nice idea to use your circular buffer as container for the average-structs.
An example:
Data:t value 0:10 5 0:20 3 0:40 7 0:50 4 1:10 4 1:15 8 1:40 9 1:55 5 2:10 3 2:15 4 2:20 6
Imagine you want to know the 1 minute and the 2 minute average every 30 seconds. As described above, you need two 1-minute average and four 2-minute averages.
At t = 0 you would have 0 for each average, obviously.
At t = 0:30 you would have:
1-min_av_1 = {5 + 3, 2}
1-min_av_2 = {5 + 3, 2}
2-min_av_1 = {5 + 3, 2}
2-min_av_2 = {5 + 3, 2}
2-min_av_3 = {5 + 3, 2}
2-min_av_4 = {5 + 3, 2}
That means you have the one minute average (5 + 3) / 2 and the same two minute average. You reset one of each averages.At t = 1:00 you would have:
1-min_av_1 = {7 + 4, 2}
1-min_av_2 = {5 + 3 + 7 + 4, 4}
2-min_av_1 = {7 + 4, 2}
2-min_av_2 = {5 + 3 + 7 + 4, 4}
2-min_av_3 = {5 + 3 + 7 + 4, 4}
2-min_av_4 = {5 + 3 + 7 + 4, 4}
You show the older 1 minute average ((5 + 3 + 7 + 4) / 4) and reset the counter. The oldes two minute average is the same again. You also reset the counter.At t = 1:30 you would have:
1-min_av_1 = {7 + 4 + 4 + 8, 4}
1-min_av_2 = {4 + 8, 2}
2-min_av_1 = {7 + 4 + 4 + 8, 4}
2-min_av_2 = {4 + 8, 2}
2-min_av_3 = {5 + 3 + 7 + 4 + 4 + 8, 6}
2-min_av_4 = {5 + 3 + 7 + 4 + 4 + 8, 6}
1 minute average: (7 + 4 + 4 +/ 4
2 minute average: (5 + 3 + 7 + 4 + 4 +/ 6
At t = 2:00 you would have:
1-min_av_1 = {9 + 5, 2}
1-min_av_2 = {4 + 8 + 9 + 5, 4}
2-min_av_1 = {7 + 4 + 4 + 8 + 9 + 5, 6}
2-min_av_2 = {4 + 8 + 9 + 5, 4}
2-min_av_3 = {9 + 5, 2}
2-min_av_4 = {5 + 3 + 7 + 4 + 4 + 8 + 9 + 5, 8}
1 minute average: (4 + 8 + 9 + 5) / 4
2 minute average: (5 + 3 + 7 + 4 + 4 + 8 + 9 + 5) / 8At t = 2:30 you would have:
1-min_av_1 = {9 + 5 + 3 + 4 + 6, 5}
1-min_av_2 = {3 + 4 + 6, 3}
2-min_av_1 = {7 + 4 + 4 + 8 + 9 + 5 + 3 + 4 + 6, 9}
2-min_av_2 = {4 + 8 + 9 + 5 + 3 + 4 + 6, 7}
2-min_av_3 = {9 + 5 + 3 + 4 + 6, 5}
2-min_av_4 = {3 + 4 + 6, 3}
1 minute average: (9 + 5 + 3 + 4 + 6) / 5
2 minute average: (7 + 4 + 4 + 8 + 9 + 5 + 3 + 4 + 6) / 9...
-
Well the Idea which you mention seems to me particle and to calculate average of measurments values is exactely same as you mention.
It's a cool idea that one have several static runing averages of each time interval and work with structures.I talk with another guy we disscuss Scenario_A & i think Scenario A can be implimented on AT39 machines with Circular_Buffer idea to use array,that's mean when we will use array then Laufindex(pointer) will be important to calculate the time average values of measurments,like when i worked with AT39 controller then my C codes have two parts like KOP Initialisation and KOP cyclic part,so KOP_Initialisation will be look like this
Array [1][1]='a';
Array [2][1]='b';
Array [3][1]='c';
Array [4][1]='d';
Array [5][1]='e';
Array [6][1]='f';
Array [7][1]='g';
Array [8][1]='h';
Array [9][1]='i';Array [1][0]='A';
Array [2][0]='B';
Array [3][0]='C';
Array [4][0]='D';
Array [5][0]='E';
Array [6][0]='F';
Array [7][0]='G';
Array [8][0]='H';
Array [9][0]='I';SetVar (Array_i,0); //SetVar is µ.C TOS function
SetVar (Array_j,0);
SetVar (Array_1,' ');
SetVar (Array_2,' ');
SetVar (Array_3,' ');i=0; // Laufparameter auf 1 setzen
j=0;
}and KOP_Cyclic part will be like this
if (IsKeyPressedNew(2)) // IsKeyPressedNew is Time Oertating System(TOS) function µ.C specific)
{
SetVar (Array_i, i);
SetVar (Array_i1, i+1);
SetVar (Array_i2, i+2);
SetVar (Array_j, j);
SetVar (Array_1, Array [i][j]); // Inhalt des Array auf Bildschirm schreiben
SetVar (Array_2, Array [i+1][j]); // Inhalt des Array auf Bildschirm schreiben
SetVar (Array_3, Array [i+2][j]); // Inhalt des Array auf Bildschirm schreiben
i=i+3;
if (i>=9) {i=1; j=1-j;}} // End if (IsKeyPressedNew(2))
}*****************************************************************************
May be it's seems to you little bit strange,but as you know all µ.C's guyshave their own environments.
This is just an example how we can realize ring buffer with AT39 type of µ.C's,but as i saw your idea is also original idea,but guy with whom i m working told me try to work with arrays and Scenario A as you mention as
"Messwerte in einem Ringspeicher gespeichert und zur Darstellung
berechnet man den Mittelwert über n Elemente in dem Ringspeicher. Das sind dann die X Minuten Mittelwerte."Any way i just want to share my idea with you,so you can also see or perhaps that also help some who like to work with such situation later on....as i said any suggestions,comments or even objections always most well come....i love when some one criticised on me
so one can improve him/herself.
Thanks for your supports.
V.G
Slfzn