Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,there

i try to create a Airplane attitude.

here is my serial port, that read from gyro.

actully its work but, my picture is slow moving and im not smooth moving.

how can i make it smooth??

First time i send Serial Like (Pitch Angle) in hex: 68 04 00 01 05
with Timer = 80;
BaudRate = 9600;

i will recive Like : 68 07 00 81 00 04 62 EE

Also
68 04 00 04 08 for Pitch,Roll,Heading and this code that will read three Angle Make it More Slow !!!!!

How can i make rotation smooth ???


C#
protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Calling the base class OnPaint
            base.OnPaint(paintEvnt);


            // Clipping mask for Attitude Indicator
            paintEvnt.Graphics.Clip = new Region(new Rectangle(0, 0, 300, 300));
            paintEvnt.Graphics.FillRegion(Brushes.Black, paintEvnt.Graphics.Clip);


            // Make sure lines are drawn smoothly
            paintEvnt.Graphics.SmoothingMode = SmoothingMode.HighQuality;

            // Create the graphics object
            Graphics gfx = paintEvnt.Graphics;
           
            // Adjust and draw horizon image
            RotateAndTranslate(paintEvnt, mybitmap1, RollAngle, 0, ptBoule, (double)(4 * PitchAngle), ptRotation, 1);

            RotateAndTranslate2(paintEvnt, mybitmap3, YawAngle, RollAngle, 0, ptHeading, (double)(4 * PitchAngle), ptRotation, 1);

            gfx.DrawImage(mybitmap2, 0, 0); // Draw bezel image
            gfx.DrawImage(mybitmap4, 75, 125); // Draw wings image


            Graphics g = CreateGraphics();
            if (serialPort1.IsOpen == true)
            {
                g.FillEllipse(Brushes.Green, 20, 350, 20, 20);
            }
            else

                g.FillEllipse(Brushes.Red, 20, 310, 20, 20);


        }

 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            // Obtain the number of bytes waiting in the port's buffer
            int bytes = serialPort1.BytesToRead;

            // Create a byte array buffer to hold the incoming data
             byte[] buffer = new byte[bytes];
            

            // Read the data from the port and store it in our buffer      
             serialPort1.Read(buffer, 0, bytes); //bytes
           
            Log(LogMsgType.Incoming, ByteArrayToHexString(buffer)); 
        
            if (bytes > 7 )//13
            {
                
                if (buffer[4] == 0)

                    PitchAngle = Convert.ToDouble(buffer[5]);
                else
                    PitchAngle = -1.0 * Convert.ToDouble(buffer[5]);

               
                Invalidate();
            }
    
        }
Posted
Comments
Sergey Alexandrovich Kryukov 20-Mar-13 3:08am    
The general idea is very clear, and basically your approach is quite correct, and this question is good. I up-voted this question by 4 (very high mark for a question, actually). The problem is the lack of fine detail of the graphic data and rendering. I can see the traditional for avionics 3D coordinate system, yaw, pitch, roll. I cannot see how transform of just some 2D bitmaps could evoke the image of the plain. I would understand it of was the 3D model, but you are using just bitmap. Do you re-generate them for each angle coordinate set? Than it could be a performance leak.

It's not 100% clear what is the buffer data read from a serial port. Is it only 3 angles? If not, it can also be a problem.

Needs more detail. Also, you could profile this solution.

—SA

1 solution

Please see my comments to the question.

Also, I can give you the alternative idea: I'm pretty much sure that you could greatly improve smoothness and performance by using WPF instead. And don't do "manual" rendering analogous to your Forms solution (which is also possible). Try 3D graphics. WPF is based on DirectX, inherently faster.

[EDIT]

I almost forgot: for serial port communications, you certainly need to use a separate thread. In this thread, of course, you won't be able to call your Invalidate or call WPF UI methods/properties from this thread. You will need to use UI thread invocation mechanism (Invoke/BeginInvoke), to delegate the calls to UI thread. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
v2
Comments
A-DeveloperZ 21-Mar-13 11:04am    
Thanks men for solution. actully here with Timer 80 i have enough Serial port and enough full packet but in (bytes > 13) i read full packet but not all full packet going inside thats why im slow do you have idea how can i make it fast and i read all full packet ??
Sergey Alexandrovich Kryukov 21-Mar-13 11:20am    
It depends very much on what's going on on the other end of you RS-232 cable, as well as communication parameters. Anyway, in your thread, you can read as much as possible at once and pre-process the data in the communication thread if needed. If data is not available, it will keep your thread in a wait state, without waste of CPU time. As data is ready for UI presentation, you can pass it in logical packets using UI thread invocation mechanism (Control.Invoke/BeginInvoke).
—SA
A-DeveloperZ 21-Mar-13 11:21am    
Thanks men
Sergey Alexandrovich Kryukov 21-Mar-13 11:30am    
Sure. Will you accept the answer formally (green button)?
—SA
A-DeveloperZ 23-Mar-13 14:36pm    
dear Alexandrovich thanks alot for u r help. but actully i didnt get my answer and yet im waiting for answer n still im working on this issue and thread as soon as possible my problem will solve with thread i will press the Green button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900