-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemograph.cs
More file actions
261 lines (233 loc) · 11.3 KB
/
demograph.cs
File metadata and controls
261 lines (233 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#region Information
/* Project : CanSat */
/* File : demograph.cs */
/* Version : 1.1 final */
/* Language : C# */
/* Summary : Graphs of certain data */
/* Creation : 26/02/2016 */
/* Updated : 25/03/2016 */
/* Author : Yeung Mun Lok */
/* Update Logs
1.1
-StreamWriter
-All IO (Writer/Reader) removed
-Cleanup
-Any debugging functions/comments and all unused code or stuff removed
1.0.3
-More on closing threads
-Apparently, the previous solutions doesnt work properly. Will focus more on this
aborting/closing of threads
-After some testing, cross form calling doesnt set the variable closeRequested in the first iteration
of the thread cycle. Only after setting flag and running one cycle of threadloop, then it detects flag changed
-ClosedThreadLoop method removed as its not needed anymore
-Solved, kind of, until something crops up
-Waste so much time for one short sentence Application.Exit called in main window
-Informs all to terminate. Calls handler FormClosing here. Sets flag, exits thread loop.
-Time stamp label added
-Changed axis labels to time instead of OADate
-Added StreamWriter
-For testing only. Remove all instances of IO when finalized; notably in Load, timer, closing, loopfunction
1.0.2
-Closing of threads
-Previous eventhandler codes are replaced as its not stable
-Added flag to determine if main window closed
-If closed, exit thread loop and close form
1.0.0
-New Windows form demograph.cs
-To chart graph from data
-Temperature and Altitude
-All X axis spans 120 seconds
-Temperature graph
-Data values shown are real temp and computed average temp
-Altitude graph
-Data values shown are altitude a.s.l
-Considering to be changed to relative altitude
-Added eventhandler to suspend threadloop
-Since suspend/resume deprecated, other solution used
-Creating a ManualResetEvent
-_event is set to pass signal to threadloop to continue when the program is running
-_event resets when main window closes, therefore stops threadloop
*/
#endregion
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace AvionicsInstrumentControlDemo
{
public partial class graphWindow : Form
{
private Thread addDataRunner;
private Random rand = new Random(); //not for testing, actually used. Do not remove
public delegate void AddDataDelegate();
public AddDataDelegate addDataDel;
private volatile bool closeRequested = false;
float realtemp=19,avgtemp=0,altitude=40,maxtemp=0,maxavgtemp=0,maxaltitude=0;//initialisation of values
public graphWindow()
{
InitializeComponent();
}
private void graphWindow_Load(object sender, EventArgs e)
{
//define a thread to add values into chart
ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop);
addDataRunner = new Thread(addDataThreadStart);
addDataDel += new AddDataDelegate(AddData);
//add series into chart
startData(null, new EventArgs());
closeRequested = false;
}
private void startData(object sender, EventArgs e)
{
//add series from min to max
DateTime minValue = DateTime.Now;
DateTime maxValue = minValue.AddSeconds(120);//change value in bracket for the axis span
//Chart 1 = Temperature. All colors can be changed
chartTemp.ChartAreas[0].AxisX.Minimum = minValue.ToOADate();
chartTemp.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate();
chartTemp.ChartAreas[0].AxisY.Minimum = 15;
chartTemp.Series.Clear();
Series newTempSeries = new Series("Temperature");
newTempSeries.ChartType = SeriesChartType.Line;
newTempSeries.BorderWidth = 2;
newTempSeries.Color = Color.OrangeRed;
newTempSeries.XValueType = ChartValueType.DateTime;
chartTemp.Series.Add(newTempSeries);
Series newTempAve = new Series("Avg Temp");
newTempAve.ChartType = SeriesChartType.Line;
newTempAve.BorderWidth = 2;
newTempAve.Color = Color.Blue;
newTempAve.XValueType = ChartValueType.DateTime;
chartTemp.Series.Add(newTempAve);
chartTemp.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
chartTemp.ChartAreas[0].AxisX.Title = "Time";
chartTemp.ChartAreas[0].AxisY.Title = "Temperature (°) ";
//Chart 2 = Altitude
chartAltitude.ChartAreas[0].AxisX.Minimum = minValue.ToOADate();
chartAltitude.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate();
chartAltitude.ChartAreas[0].AxisY.Minimum = 40;
chartAltitude.Series.Clear();
Series newAltitudeSeries = new Series("Altitude");
newAltitudeSeries.ChartType = SeriesChartType.Line;
newAltitudeSeries.BorderWidth = 2;
newAltitudeSeries.Color = Color.Green;
newAltitudeSeries.XValueType = ChartValueType.DateTime;
chartAltitude.Series.Add(newAltitudeSeries);
chartAltitude.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
chartAltitude.ChartAreas[0].AxisX.Title = "Time";
chartAltitude.ChartAreas[0].AxisY.Title = "Altitude above sea level (m) ";
//start thread to add data into chart
addDataRunner.Start();
}
private void AddDataThreadLoop()//Problems problems
{
//closeRequested not set in the below method. if CloseThreadLoop called in this form, it changes.
//try and catch so when the form closes, invoke throws exception
while (!closeRequested)
{
try {
chartTemp.Invoke(addDataDel);
}
catch (ObjectDisposedException)
{
break;
}
Thread.Sleep(1000);
} //do loop while flag is not set
}
public void AddData()
{
DateTime timeStamp = DateTime.Now; //get current time
label1.Text = timeStamp.ToLongTimeString(); //write into label
AddNewPoint(timeStamp, chartTemp.Series[0]); //add current temperature point to graph
AddAvgPoint(timeStamp, chartTemp.Series[1]); //add avg temperature point to graph
AddAltiPoint(timeStamp, chartAltitude.Series[0]); //add altitude point to graph
}
public void AddAltiPoint(DateTime timeStamp, Series ptSeries)
{
double newVal = 0;
if (ptSeries.Points.Count > 0)
{
newVal = ptSeries.Points[ptSeries.Points.Count - 1].YValues[0] + ((rand.NextDouble() * 2) - 1);
}
if (newVal < 0)
newVal = 0;
ptSeries.Points.AddXY(timeStamp.ToOADate(), altitude);
double removeBefore = timeStamp.AddSeconds((double)(90) * (-1)).ToOADate();
while (ptSeries.Points[0].XValue < removeBefore)
{
ptSeries.Points.RemoveAt(0);
}
chartAltitude.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
chartAltitude.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate();
chartAltitude.ChartAreas[0].AxisY.Maximum = Convert.ToInt32(maxaltitude) + 1;
chartAltitude.Invalidate();
}
public void AddAvgPoint(DateTime timeStamp, Series ptSeries)
{
double newVal = 0;
if (ptSeries.Points.Count > 0)
{
newVal = ptSeries.Points[ptSeries.Points.Count - 1].YValues[0] + ((rand.NextDouble() * 2) - 1);
}
if (newVal < 0)
newVal = 0;
ptSeries.Points.AddXY(timeStamp.ToOADate(), avgtemp);
double removeBefore = timeStamp.AddSeconds((double)(90) * (-1)).ToOADate();
while (ptSeries.Points[0].XValue < removeBefore)
{
ptSeries.Points.RemoveAt(0);
}
chartTemp.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
chartTemp.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate();
chartTemp.ChartAreas[0].AxisY.Maximum = Convert.ToInt32(maxavgtemp) + 1;
chartTemp.Invalidate();
}
public void AddNewPoint(DateTime timeStamp, Series ptSeries)
{
double newVal = 0;
if (ptSeries.Points.Count > 0)
{
newVal = ptSeries.Points[ptSeries.Points.Count - 1].YValues[0] + ((rand.NextDouble() * 2) - 1);
}
if (newVal < 0)
newVal = 0;
ptSeries.Points.AddXY(timeStamp.ToOADate(), realtemp);
double removeBefore = timeStamp.AddSeconds((double)(90) * (-1)).ToOADate();
while (ptSeries.Points[0].XValue < removeBefore)
{
ptSeries.Points.RemoveAt(0);
}
chartTemp.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
chartTemp.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate();
chartTemp.ChartAreas[0].AxisY.Maximum = Convert.ToInt32(maxtemp) + 1;
chartTemp.Invalidate();
}
public void setTemp(float temp)
{
realtemp = temp;
if (avgtemp == 0)
avgtemp = temp;
avgtemp = (avgtemp + realtemp) / 2;
if (temp >= maxtemp)
maxtemp = temp;
if (avgtemp >= maxavgtemp)
maxavgtemp = avgtemp;
}
public void setAltitude(float alti)
{
altitude = alti;
if (alti >= maxaltitude)
maxaltitude = alti;
}
private void hideButton_Click(object sender, EventArgs e)
{
Hide();
}
private void graphWindow_FormClosing(object sender, FormClosingEventArgs e)
{
closeRequested = true;
}
}
}