GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Nov 24, 2014 10:34 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Real-Time Plotting using GTK_PLOT (Re: GtkPlot resize)
PostPosted: Wed Dec 07, 2011 9:49 am 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Goodmorning,
I was wondering if anyone knows how to resize a gtkplot without redrawing the plot.
You know that making a gtk_plot_resize implies the redrawing of the entire plot: I'm just trying to make a resize of the axis without update the plot inside it.

Tanks in advance.


Last edited by Nyquist on Fri Dec 16, 2011 1:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: GtkPlot resize
PostPosted: Tue Dec 13, 2011 11:19 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I think if your axis is in a different gtk region than everything else then you can do this but I think this could go badly wrong considering plots can span across axes and you'd likely have to redo them too anyway.
For what reason would you not want to rescale the plot along with the axis???


Top
 Profile  
 
 Post subject: Real-Time Plotting issues using GTK_PLOT (Re: GtkPlot resize
PostPosted: Fri Dec 16, 2011 1:37 pm 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Ok, let me explain from the beginning.

I'm trying to develop a real-time plotting gui which picks up data from the serial port and displays it real time using gtk_extra.

Firstly here follows some specifications for the project:
- Acquisition of an analog signal is done with a dsPIC30f3013 so the ADC works with 12 bits.
- The Sampling Rate is set to 1kHz while the baud rate is 57600 Baud.
- 12 Bits, towards serial communication, are obviously split in two Bytes, one containing the high part of them (4 bits) and the other with the lowest 8 bits (the hight one is transmitted first).
- Plot updating rate should be set to 50 Hz so that every 20 samples another point in the plot should be painted.

Inside the gui program, there is a g_thread who simply reads from serial (NONBLOCKING mode) and fills a buffer with split bytes.
At the same time there is another thread (protected with gdk global lock) who does the updating of the gtk_plot.

Inside the serial acquisition thread, after having filled 40 bytes (20 samples *2) a semaphore unlocks the updating thread functions which has exactly 20ms to perform the action. If the drawing takes longer, another semaphore blocks the acquisition thread until the plotting isn't completely performed. Obviously the down-sampling from 1kHz to 50Hz requires high order low pass filters. In particular I'm using a 34-th order Cheby LPF at 12Hz in the same updating thread.

Basically I have the need to build a gui that maintains each single acquired sample (at 50Hz) on the plot, from the beginning of the session to the end if it, with a maximum duration of 10 minutes. Taking into account that drawing the same pixel would be a waste, I stated that each second on the plot has a width of 50 pixel. So, if you use a gtk_plot inside a gtk_plot_canvas, the maximum allocation width for a gtk_canvas is about 32000 pixels which means a bit less than 11 minutes of plotting for a 50Hz plotting rate where each second is drawn with 50 pixels.

Each action on such a huge gtk_plot_canvas is very very heavy so that a dynamical plot child resize performed in real-time is unacceptable. To have a scrollable plot I have put the canvas inside a frame inside a scrolled_window through a viewport. Making a size_request to the frame lets me perform something similar to a resize of the plot in real-time, without any particular load for the processor (and this is finally related to the original question of the first post).

In order to optimize the time needed to draw a new sample on the plot I have searched through various ways. Now I'm doing this by calling gdk_window_invalidate_rect using a rect which is changed dynamically, depending on the position of the sample to draw on the plot.

Although this is a quite low-weighted plotting method, it isn't enough. In fact this requires more than 20ms to plot a sample.
I think that this is related to the need to call gdk_thread_enter() and gdk_thread_leave() to protect plotting functions such as gdk_window_invalidate_rect.

Well, consider that I'm absolutely a newbie to gtk_programming. I really don't know if using a separated protected thread for plotting is best than, let say, something similar to gtk_idle function which is already protected.
For this peace of advice I'm deferring to you. Especially to everyone that has been so brave to follow me until the end XD

Thanks.

(PS: I'm going to change the name of the thread)


Top
 Profile  
 
 Post subject: Re: Real-Time Plotting using GTK_PLOT (Re: GtkPlot resize)
PostPosted: Fri Dec 16, 2011 9:44 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
Have you looked at the real time plot demo in the examples ?


Top
 Profile  
 
 Post subject: Re: Real-Time Plotting using GTK_PLOT (Re: GtkPlot resize)
PostPosted: Sat Dec 17, 2011 10:36 am 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Yes, sure. I started form there almost three months ago.
However, I have found a turn-around for 32000 xlib limit. I'm just tiling canvases one near the other in a table so that I can create a "merged" canvas bigger than 32k. It works.

Moreover, I have also discovered that timing delays on plotting were caused by an approximation of the sampling time which indeed isn't 0.001s but 0.00104s . On 100s it means 4 seconds of delay.
So my plotting system isn't computationally hard but the question remains the same:

which is the best way to perform these timing-constrained actions (such as real time plotting)? A separate thread protected with gdk main lock or a gdk_threads_add_idle_full () with G_PRIORITY_HIGH_IDLE?


Top
 Profile  
 
 Post subject: Re: Real-Time Plotting using GTK_PLOT (Re: GtkPlot resize)
PostPosted: Sat Dec 17, 2011 9:00 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
I really like your project! I've been wanting to do this for some time. I recently bought a Arduino compatible board but it has an onboard temp sensor; Lipo battery, solar panel input and microSD.
Atmel ATmega328P (8-bit MCU)
onboard 10-bit ADC
Maxim DS3231 I²C RTC, Digital Temp Sensor Output: ±3°C Accuracy
http://www.seeedstudio.com/depot/seeeduino-stalker-v2-p-727.html?cPath=132_133
So far though I've just turned on the onboard LED with a GtkButton and in another project, retrieved the temp. and displayed it in a GtkEntry (one shot, not continuous)

I'm not sure how to advise you about the thread protection. It seams your requirement of 50Hz sample rate may be unattainable with GtkExtra. Here's a thread that talks about a solution for thread protection but they only needed up to 32Hz but maybe it can work for you. http://ubuntuforums.org/showthread.php?t=1463078

Your on Linux right? If so what version?

Here's a thread where someone was also grabbing temp and humidity from a PIC and plotting it but not in real time. Tadeboro gave some code which uses Cario directly. That might be what you need to get your required speed. Paul Childs mentioned in the same thread that he's "also developed a few plotting widgets customized for scientific publication quality graphing" I would PM him and or Tadeboro
http://www.gtkforums.com/viewtopic.php?t=3301#8764

I would also have a look at these:
"Goffice is a library including a 2D graph component with a cairo based backend. This library is used by Gnumeric and Gnucash."
"PLplot is a 2D and 3D plotting library with several cairo device drivers. Currently these include xwindows, ps, pdf and png."
http://cairographics.org/examples/

Please post back or PM me. I would really like to see this work!


Top
 Profile  
 
 Post subject: Re: Real-Time Plotting using GTK_PLOT (Re: GtkPlot resize)
PostPosted: Sat Dec 17, 2011 10:39 pm 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Thank you very much for your reply.

While I was on my first steps for this project I had visited almost the same links. Unfortunately, although I'm aware that there exist libraries that perform a lighter drawing, it's to late to change completely the plotting system. I have to keep on using gtk_extra because this project must be ended before the first weeks of the new year. Moreover, I found out that 50Hz seems to be an affordable timing constraint: the drawing keep quite well this pace and there remains also a time slice where I can perform a 34th order filtering and other stuff.

I'm using Ubuntu 10.10. On one hand because I love Gnome2 and on the other hand because it runs gtk2.0 which is needed to compile gtkextra3.

I'm working on the code in these days and since I'm really not a good programmer it would probably be quite confused. It's not commented at all and I can't really beat on how huge is the memory leak among the program XD (I always forget to free objects).
For these aesthetic reasons I'm not ready at all to post my code now. However, when I will state it as "working reasonably well" I will share it in this forum!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group