real-time measuring and plotting into a graph. HOW?

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: real-time measuring and plotting into a graph. HOW?

Post by infratec »

Hi Wim,

you forgot something at the end of your cls():

Code: Select all

SetGadgetState(1, ImageID(0))
Bernd
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

Re: real-time measuring and plotting into a graph. HOW?

Post by wimapon »

How stupid...........
But, now it works... thanks

I will still report here when the complete measuring program works

Wim
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: real-time measuring and plotting into a graph. HOW?

Post by djes »

For such real-time drawing, may I suggest you to consider using screen library, and the use of OpenWindowedScreen() and ClearScreen() commands. It's easy and fast, perfect for visualisation. But images commands are better for cross-platform development.
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: real-time measuring and plotting into a graph. HOW?

Post by graph100 »

If you want a quick tool to display graphics, i've done something for this some time ago (here)
I have put it in module to use it simpler :

Code: Select all

DeclareModule Graph
	
	Structure Graph_pt_point_d
		x.d
		y.d
	EndStructure
	
	Structure Graph_pt
		*graph.Graph
		
		color.l
		line.b
		
		List pt.d()
	EndStructure
	
	Structure Graph
		pos.Graph_pt_point_d
		dimension.POINT
		
		nb_point.l
		
		List list_des_pt.Graph_pt()
		
		maxi.d
		mini.d
		
		ratio_w.d
		ratio_h.d
		
		Nom.s
	EndStructure
	
	Declare.i Graph_Init(x, y, width, height, nb_point, Nom.s = "")
	Declare.i Graph_AddSerie(*graph.Graph, color.l, Line.b = #False)
	Declare.i Graph_FreeSerie(*graph.Graph, *adresse_serie)
	Declare Graph_AddPoint(*adresse_serie.Graph_pt, value.d)
	Declare Graph_Draw(*graph.Graph)
EndDeclareModule

Module Graph
	
	
	Procedure.d Maxi_delta(value.d, delta.d)
		If delta = 0
			ProcedureReturn Round(value, #PB_Round_Up) + 0.5
		Else
			p10.d = Pow(10, Round(Log10(Abs(delta)), #PB_Round_Down))
			ProcedureReturn Round(value / p10, #PB_Round_Up) * p10
		EndIf
	EndProcedure
	
	Procedure.d Mini_delta(value.d, delta.d)
		If delta = 0
			ProcedureReturn Round(value, #PB_Round_Down) - 0.5
		Else
			p10.d = Pow(10, Round(Log10(Abs(delta)), #PB_Round_Down))
			ProcedureReturn Round(value / p10, #PB_Round_Down) * p10
		EndIf
	EndProcedure
	
	
	
	Procedure.i Graph_Init(x, y, width, height, nb_point, Nom.s = "")
		*graph.Graph = AllocateMemory(SizeOf(Graph))
		InitializeStructure(*graph, Graph)
		
		*graph\pos\x = x
		*graph\pos\y = y
		*graph\dimension\x = width
		*graph\dimension\y = height
		
		*graph\nb_point = nb_point
		
		*graph\ratio_w = *graph\dimension\x / *graph\nb_point
		
		*graph\Nom = Nom
		
		ProcedureReturn *graph
	EndProcedure
	
	Procedure.i Graph_AddSerie(*graph.Graph, color.l, Line.b = #False) ; retourne l'adresse de la série. A conserver pour ajouter des points
		*new_serie = AddElement(*graph\list_des_pt())
		
		InitializeStructure(*new_serie, Graph_pt)
		*graph\list_des_pt()\color = color
		*graph\list_des_pt()\graph = *graph
		*graph\list_des_pt()\line = Line
		
		ProcedureReturn *new_serie
	EndProcedure
	
	Procedure.i Graph_FreeSerie(*graph.Graph, *adresse_serie) ; retourne l'adresse de la série. A conserver pour ajouter des points
		ChangeCurrentElement(*graph\list_des_pt(), *adresse_serie)
		
		FreeList(*graph\list_des_pt()\pt())
		
		DeleteElement(*graph\list_des_pt())
	EndProcedure
	
	Procedure Graph_AddPoint(*adresse_serie.Graph_pt, value.d)
		LastElement(*adresse_serie\pt())
		AddElement(*adresse_serie\pt())
		*adresse_serie\pt() = value
		
		If ListSize(*adresse_serie\pt()) > *adresse_serie\graph\nb_point
			FirstElement(*adresse_serie\pt())
			DeleteElement(*adresse_serie\pt())
		EndIf
	EndProcedure
	
	Procedure Graph_Draw(*graph.Graph)
		Line(*graph\pos\x - 1, *graph\pos\y - 1, *graph\dimension\x + 2, 1, #Green)
		Line(*graph\pos\x - 1, *graph\pos\y - 1, 1, *graph\dimension\y + 2, #Green)
		Line(*graph\pos\x - 1, *graph\pos\y - 1 + *graph\dimension\y + 2, *graph\dimension\x + 2, 1, #Green)
		Line(*graph\pos\x - 1 + *graph\dimension\x + 2, *graph\pos\y - 1, 1, *graph\dimension\y + 2, #Green)
		
		If *graph\Nom
			
			Protected titre$ = " " + *graph\Nom + " "
			Protected w = TextWidth(titre$), h = TextHeight(titre$)
			Protected x.d = *graph\pos\x + *graph\dimension\x / 2 - w / 2, y.d = *graph\pos\y - h / 2
			
			DrawText(x ,y , titre$, #Green)
			
			Line(x - 1, y - 1, w + 1, 1, #Green)
			Line(x - 1, y - 1, 1, h + 1, #Green)
			Line(x - 1, y - 1 + h + 1, w + 2, 1, #Green)
			Line(x - 1 + w + 1, y - 1, 1, h + 2, #Green)
			
		EndIf
		
		DrawText(*graph\pos\x + *graph\dimension\x + 2, *graph\pos\y - 1, StrD(*graph\maxi), #Green)
		DrawText(*graph\pos\x + *graph\dimension\x + 2, *graph\pos\y - 1 + *graph\dimension\y + 2 - TextHeight(" "), StrD(*graph\mini), #Green)
		
		
		*graph\ratio_h = *graph\dimension\y / (*graph\maxi - *graph\mini)
		
		mini.d = *graph\mini
		
		If Sign(*graph\maxi) <> Sign(*graph\mini)
			Line(*graph\pos\x, *graph\pos\y + *graph\dimension\y + mini * *graph\ratio_h, *graph\dimension\x, 1, #Green)
			
			DrawText(*graph\pos\x + *graph\dimension\x + 2, *graph\pos\y + *graph\dimension\y + mini * *graph\ratio_h - TextHeight(" ") / 2, "0", #Green)
		EndIf
		
		*graph\maxi = -Infinity()
		*graph\mini = Infinity()
		
		ForEach *graph\list_des_pt()
			index = 0
			
			If *graph\list_des_pt()\line = #False
				
				ForEach *graph\list_des_pt()\pt()
					If *graph\list_des_pt()\pt() > *graph\maxi : *graph\maxi = *graph\list_des_pt()\pt() : EndIf
					If *graph\list_des_pt()\pt() < *graph\mini : *graph\mini = *graph\list_des_pt()\pt() : EndIf
					
					Line(*graph\pos\x + index * *graph\ratio_w, *graph\pos\y + *graph\dimension\y - (*graph\list_des_pt()\pt() - mini) * *graph\ratio_h, 1, 1, *graph\list_des_pt()\color)
					
					index + 1
				Next
				
			Else
				*adr.Double = FirstElement(*graph\list_des_pt()\pt())
				old_x.d = *graph\pos\x + index * *graph\ratio_w
				old_y.d = *graph\pos\y + *graph\dimension\y - (*graph\list_des_pt()\pt() - mini) * *graph\ratio_h
				
				ForEach *graph\list_des_pt()\pt()
					If *graph\list_des_pt()\pt() > *graph\maxi : *graph\maxi = *graph\list_des_pt()\pt() : EndIf
					If *graph\list_des_pt()\pt() < *graph\mini : *graph\mini = *graph\list_des_pt()\pt() : EndIf
					
					x.d = *graph\pos\x + index * *graph\ratio_w
					y.d = *graph\pos\y + *graph\dimension\y - (*graph\list_des_pt()\pt() - mini) * *graph\ratio_h
					
					LineXY(x, y, old_x, old_y, *graph\list_des_pt()\color)
					
					old_x = x
					old_y = y
					
					index + 1
				Next
				
			EndIf
			
			If LastElement(*graph\list_des_pt()\pt())
				DrawText(*graph\pos\x + *graph\dimension\x + 2, *graph\pos\y + *graph\dimension\y - (*graph\list_des_pt()\pt() - mini) * *graph\ratio_h - TextHeight(" ") / 2, StrD(*graph\list_des_pt()\pt()), *graph\list_des_pt()\color)
			EndIf
		Next
		
		
		delta.d = *graph\maxi - *graph\mini
		
		*graph\maxi = Maxi_delta(*graph\maxi, delta)
		*graph\mini = Mini_delta(*graph\mini, delta)
		
		If delta = 0 : delta = 1 : EndIf
		
	EndProcedure
	
	
EndModule


CompilerIf #PB_Compiler_IsMainFile = #True

OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(0, 10, 10, 780, 580)

AddKeyboardShortcut(0, #PB_Shortcut_Escape, 0)
AddKeyboardShortcut(0, #PB_Shortcut_Up, 1)
AddKeyboardShortcut(0, #PB_Shortcut_Down, 2)

UseModule Graph

;{ init graphics

*graph.Graph = Graph_Init(10, 10, 700, 200, 300, "Sinus, Cosinus")
*ad_sinus = Graph_AddSerie(*graph, #Blue, #True)
*ad_cosinus = Graph_AddSerie(*graph, RGB(200, 200, 255))

*graph_fps.Graph = Graph_Init(10, 230, 700, 200, 1000, "fps, _dt_lim")
*ad_fps = Graph_AddSerie(*graph_fps, #Blue)
*ad__dt_lim = Graph_AddSerie(*graph_fps, RGB(200, 200, 255))

;}

_dt_lim = 20
time = ElapsedMilliseconds()

sinus = 0
cosinus = 1

Repeat
	
	;{ event
	Repeat
		event = WindowEvent()
		Select event
			Case #PB_Event_Menu
				Select EventMenu()
					Case 0
						event = #PB_Event_CloseWindow
						
					Case 1
						_dt_lim + 1
						If _dt_lim > 50 : _dt_lim = 50 : EndIf
						
					Case 2
						_dt_lim - 1
						If _dt_lim < 1 : _dt_lim = 1 : EndIf
						
						
				EndSelect
		EndSelect
		
		If event = #PB_Event_CloseWindow
			End
		EndIf
		
	Until event = 0
	;}
	
	;{ ajustement du fps
	dt = ElapsedMilliseconds() - time
	If dt < _dt_lim
		Delay(_dt_lim - dt)
	EndIf
	dt = ElapsedMilliseconds() - time
	time = ElapsedMilliseconds()
	;}
	
	Graph_AddPoint(*ad_sinus, 5*Cos(time/10000)*Sin(time/1000))
	Graph_AddPoint(*ad_cosinus, 2*Cos(time/1000))
	
	Graph_AddPoint(*ad_fps, 1000/dt)
	Graph_AddPoint(*ad__dt_lim, _dt_lim)
	
	;{ dessin
	
	StartDrawing(CanvasOutput(0))
	Box(0, 0, OutputWidth(), OutputHeight(), 0)
	
	Graph_Draw(*graph)
	Graph_Draw(*graph_fps)
	
	DrawText(10, 500, "[UP], [DOWN] : Modify the FPS limiter", $FFFFFF, 0)
	
	StopDrawing()
	
	;}
	
ForEver

CompilerEndIf
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

Re: real-time measuring and plotting into a graph. HOW?

Post by wimapon »

Hi folks,
thanks anyway for your help.
I do greatly apreciate it.

but you have to know, i am a very simple basic programmer.
My interest is not to make the most beautifull programs.
I use programming to reach my goal. Just as a tool.

Infratec knows exactly what i need: a simple basical trick to do something.
I will change that trick myself to what my programs need.

My programs have often more than 10 000 lines.....
for example: i made a purebasic program which controls my antenna-rotators
to follow a satelite for communicating whith it. It only needs the keppler
parameters of a satelite and the computer does the rest.
I also made a program which handles an interferometry radio telescope to make
a map of the whole radio-sky. handling about 100 terrabyte of collected data....

For me it is great pleasure that i can ask in this forum a solution of a basical
programming problem.

If you should see my programs you will think: what an amateur..
Yes i am an amateur... hi hi
But they do the job.

So anyhow.. thanks for you help

and for Infratec: my real-time-measuring-program is finished and works
perfectly. thanks you very much again.. a lot of flowers for you

Wim
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: real-time measuring and plotting into a graph. HOW?

Post by infratec »

my real-time-measuring-program is finished and works perfectly.
Well done :!:
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: real-time measuring and plotting into a graph. HOW?

Post by djes »

Here's an old simple example showing you the basis of screen drawing operations.

Code: Select all

; Calculate next Sinus & Cosinus (imagine a rectangle, area is constant, side are calculated based on each other)

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 340, 285, "Plot example", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  If OpenWindowedScreen(WindowID(0), 10, 70, 320, 200, 0, 0, 0) = 0
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf
  
EndIf

Global a.d, b.d, d.d

Procedure.d NextSinCos()
  
  Define a_.d, b_.d
  a_ = a + b * d
  b_ = b - a_ * d
  
  a = a_
  b = b_
  
EndProcedure

a.d = Sin(0) ;=0
b.d = Cos(0) ;=1
d.d = 0.05
x = 160 : y = 100

Repeat
  
  Repeat
    ; Always process all the events to flush the queue at every frame
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_CloseWindow
        Quit = 1
        
    EndSelect
    
  Until Event = 0 ; Quit the event loop only when no more events are available
  
  ExamineMouse()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)    And y > 0   : y -3 : EndIf  
  If KeyboardPushed(#PB_Key_Down)  And y < 280 : y +3 : EndIf  
  If KeyboardPushed(#PB_Key_Left)  And x > 0   : x -3 : EndIf  
  If KeyboardPushed(#PB_Key_Right) And x < 300 : x +3 : EndIf  
  
  NextSinCos()
  Debug a
  Debug b
  
  xp = x + 32 * a
  yp = y + 32 * b
  
  ; Clear the screen and draw our sprites
  ;ClearScreen(RGB(0,0,0))

  If xp > 0 And xp < 320 And yp > 0 And yp < 200
 
    StartDrawing(ScreenOutput())
    Plot(xp, yp, RGB(Random(16) * $F, Random(16) * $F, Random(16) * $F))
    StopDrawing()
    
  EndIf
 
  FlipBuffers()       ; Inverse the buffers (the back become the front (visible)...) and we can do the rendering on the back
  
Until  Quit Or KeyboardPushed(#PB_Key_Escape)
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: real-time measuring and plotting into a graph. HOW?

Post by graph100 »

wimapon wrote:but you have to know, i am a very simple basic programmer.
My interest is not to make the most beautifull programs.
I use programming to reach my goal. Just as a tool.
I understand you want to keep it simple :) , it wasn't my intention to drown you under loads of code :| .
This is why I put it in a way in which the code just need to be included in the main, then use 3 functions and it works !

Anyway, somebody may use it for another thing, it's not a loss :wink: as, I said, it's a quick all-done-tool
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: real-time measuring and plotting into a graph. HOW?

Post by blueb »

graph100 wrote: I understand you want to keep it simple :) , it wasn't my intention to drown you under loads of code :| .
This is why I put it in a way in which the code just need to be included in the main, then use 3 functions and it works !

Anyway, somebody may use it for another thing, it's not a loss :wink: as, I said, it's a quick all-done-tool
Au contraire mon ami ... :D
I am a graphing 'beginner' and found it very educational.
But I agree... simplicity is a good thing.

Thanks again
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: real-time measuring and plotting into a graph. HOW?

Post by davido »

@djes
Nice example. :D
Very useful. I'll learn a lot from it. Thank you.
DE AA EB
Post Reply