Beispiel #1
0
        // Serial port callback function to handle the receipt of messages from the Motor Controller
        private void MRSMainControllerSerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                MRSMainControllerSerialPort.Read(inBuffer, 0, COMM_BUFFER_SIZE);
                commTimeoutErrorFlag = false;      // If no exception was thrown then we should have receiced a complete buffer (COMM_BUFFER_SIZE bits)

                // Check that the first byte contains the CommStartByte
                if (inBuffer[0x00] != CommStartByte)
                {
                    commFramingErrorFlag = true;
                    Console.WriteLine("Serial port framing error");
                    return;
                }
                else
                {
                    commFramingErrorFlag = false;
                }

                // Check that the checksums match
                Byte checkSum = 0;
                for (int i = 0; i < COMM_BUFFER_SIZE - 1; ++i)
                {
                    checkSum += inBuffer[i];
                }
                if (checkSum == inBuffer[COMM_BUFFER_SIZE - 1])
                {
                    commChecksumErrorFlag = false;
                }
                else
                {
                    commChecksumErrorFlag = true;
                    Console.WriteLine("Serial port checksum error");
                    return;
                }

                // If no errors then set flag indicating that a valid message has been received
                mrsmcMessageReceived = true;
            }
            catch (System.TimeoutException)
            {
                commTimeoutErrorFlag = true;
                Console.WriteLine("Serial port timeout");
                mrsmcMessageReceived = false;
            }
        }
Beispiel #2
0
        private void testCommsToolStripButton_Click(object sender, EventArgs e)
        {
            BuildCommMessage(TRexStatMsgType, dummy);

            if (!mcspConnectCheckBox.Checked)
            {
                mcspConnectCheckBox.Checked = true;
            }

            try
            {
                MRSMainControllerSerialPort.Write(outBuffer, 0, COMM_BUFFER_SIZE);
            }
            catch (InvalidOperationException ioe)
            {
                Console.WriteLine(ioe.GetType().Name + ": " + ioe.Message);
            }
        }
Beispiel #3
0
 private void modeToolStripButton_Click(object sender, EventArgs e)
 {
     foreach (ToolStripButton tsb in modeButtons)
     {
         tsb.Checked = tsb == sender;
         if (tsb.Checked)
         {
             Byte[] mode = new Byte[1];
             mode[0] = Convert.ToByte(tsb.Tag.ToString());
             BuildCommMessage(SetModeMsgType, mode);
             try
             {
                 MRSMainControllerSerialPort.Write(outBuffer, 0, COMM_BUFFER_SIZE);
             }
             catch (InvalidOperationException ioe)
             {
                 Console.WriteLine(ioe.GetType().Name + ": " + ioe.Message);
             }
         }
     }
 }
Beispiel #4
0
 private void mcspConnectCheckBox_CheckedChanged(object sender, EventArgs e)
 {
     if (mcspConnectCheckBox.Checked)
     {
         try
         {
             mcspTimer.Enabled = true;
             MRSMainControllerSerialPort.Open();
         }
         catch (IOException ioe)
         {
             Console.WriteLine(ioe.GetType().Name + ": " + ioe.Message);
             mcspConnectCheckBox.Checked = false;
         }
     }
     else
     {
         if (MRSMainControllerSerialPort.IsOpen)
         {
             MRSMainControllerSerialPort.Close();
             mcspTimer.Enabled = false;
         }
     }
 }
Beispiel #5
0
        private void mcspTimer_Tick(object sender, EventArgs e)
        {
            if (ShowAllMRSMCCommandBufferUpdates)
            {
                DisplayCommBuffers();
            }

            // Display communication error status
            commErrorDisplayLabel.ForeColor = Color.Black;
            if (mcspConnectCheckBox.Checked)
            {
                commErrorDisplayLabel.Text = "Connected - no errors";
            }
            else
            {
                commErrorDisplayLabel.Text = "Not connected";
            }
            if (commTimeoutErrorFlag)
            {
                commErrorDisplayLabel.ForeColor = Color.Red;
                commErrorDisplayLabel.Text      = "Serial port timeout";
                commTimeoutErrorFlag            = false;
            }
            if (commFramingErrorFlag)
            {
                commErrorDisplayLabel.ForeColor = Color.Red;
                commErrorDisplayLabel.Text      = "Serial port framing error";
                commFramingErrorFlag            = false;
            }
            if (commChecksumErrorFlag)
            {
                commErrorDisplayLabel.ForeColor = Color.Red;
                commErrorDisplayLabel.Text      = "Serial port checksum error";
                commChecksumErrorFlag           = false;
            }
            if (commTimeoutErrorFlag)
            {
                commErrorDisplayLabel.ForeColor = Color.Red;
                commErrorDisplayLabel.Text      = "Serial port timeout";
            }
            // Handle any new message from the MRS Master Controller
            if (mrsmcMessageReceived)
            {
                Byte mrsmcMessageType = inBuffer[0x01];
                switch (mrsmcMessageType)
                {
                case TextMsgMsgType:
                    String message = "";
                    for (int i = 2; i < COMM_BUFFER_SIZE - 1; ++i)
                    {
                        if (inBuffer[i] != 0x00)
                        {
                            message += (char)inBuffer[i];
                        }
                    }
                    textMessageTextBox.AppendText(message);
                    Console.Write(message);
                    break;

                case SetModeMsgType:

                    break;

                case TRexCmdMsgType:

                    break;

                case TRexStatMsgType:
                    for (int i = 0; i < TRex.TREX_STATUS_BUFFER_SIZE; ++i)
                    {
                        tRex.StatusBuffer[i] = inBuffer[i + 2];
                    }
                    if (ShowInBufferUpdates)
                    {
                        DisplayInBuffer();
                    }
                    mcStatusRequestSent = false;
                    // Displays will be updated below in this timer event handler
                    break;

                default:

                    break;
                }

                mrsmcMessageReceived = false;
            }         // Serial Communication with Master Controller

            // If a new motor controller command is not pending then request a motor controller status update
            if (!newTRexMotorControllerCommand)
            {
                ++displayCount;

                if (displayCount >= 10)
                {
                    if (mcStatusRequestSent)
                    {
                        commErrorDisplayLabel.ForeColor = Color.Orange;
                        commErrorDisplayLabel.Text      = "MC status update timeout";
                        Console.WriteLine("MC status update timeout");
                        mcStatusRequestSent = false;
                    }
                    else
                    {
                        BuildCommMessage(TRexStatMsgType, dummy);
                        try
                        {
                            MRSMainControllerSerialPort.Write(outBuffer, 0, COMM_BUFFER_SIZE);
                        }
                        catch (InvalidOperationException ioe)
                        {
                            Console.WriteLine(ioe.GetType().Name + ": " + ioe.Message);
                        }
                        mcStatusRequestSent = true;
                    }
                    displayCount = 0;
                }
            }

            // If any part of the command set for the motor controller has been updated then build a new message and send it
            if (newTRexMotorControllerCommand)
            {
                BuildCommMessage(TRexCmdMsgType, tRex.CommandBuffer);
                try
                {
                    MRSMainControllerSerialPort.Write(outBuffer, 0, COMM_BUFFER_SIZE);
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.GetType().Name + ": " + ioe.Message);
                }
                if (ShowOutBufferUpdates)
                {
                    DisplayOutBuffer();
                }
                newTRexMotorControllerCommand = false;
                displayCount = 0;
            }

            // Update motor control graphic display
            using (Graphics g = Graphics.FromImage(steeringPictureBox.Image))
            {
                // Create pen & brush
                Pen        blackPen   = new Pen(Color.Black, 1);
                SolidBrush blackBrush = new SolidBrush(Color.Black);

                g.DrawEllipse(blackPen, 43, 15, 10, 20);

                // Create rectangle for ellipse
                Rectangle rect = new Rectangle(0, 0, 95, 49);

                // Create start and sweep angles.
                float startAngle = 265.0F;
                float sweepAngle = 10.0F;

                // Draw pie to screen
                g.FillPie(blackBrush, rect, startAngle, sweepAngle);
            }
            steeringPictureBox.Invalidate();

            // Update motor controller data displays
            throttleSettingDisplayLabel.Text = tRex.Throttle.ToString("000");
            lMotSpeedDisplayLabel.Text       = tRex.LMotSpeed.ToString("000");
            rMotSpeedDisplayLabel.Text       = tRex.RMotSpeed.ToString("000");

            tRexVSupplyDisplayLabel.Text = (tRex.BatV / 100.0).ToString("N2");
            leftIMotorDisplayLabel.Text  = tRex.LMotI.ToString("###0");
            rightIMotorDisplayLabel.Text = tRex.RMotI.ToString("###0");
            tRexXAccelDisplayLabel.Text  = "X: " + tRex.AccelX.ToString("###0");
            tRexYAccelDisplayLabel.Text  = "Y: " + tRex.AccelY.ToString("###0");
            tRexZAccelDisplayLabel.Text  = "Z: " + tRex.AccelZ.ToString("###0");
        }