Пример #1
0
        private void SoftMove(int steps, int direction)
        {
            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            Messages.Msg_Command cmdMove = new Messages.Msg_Command {
                command = Messages.COMMAND_TYPES.MOVE
            };
            cmdMove.param1 = (byte)(steps >> 8);
            cmdMove.param2 = (byte)(steps & 0x00FF);
            cmdMove.param3 = (byte)direction;

            byte[] msgMove = serialMessages.GeneratePacket(cmdMove.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgMove, 0, msgMove.Length);
            }
        }
Пример #2
0
        private void btnControlStep_Click(object sender, EventArgs e)
        {
            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            Messages.Msg_Command cmdStep = new Messages.Msg_Command {
                command = Messages.COMMAND_TYPES.STEP
            };

            cmdStep.param1 = chkChangeDirection.Checked == true ? (byte)1 : (byte)0;

            byte[] msgStep = serialMessages.GeneratePacket(cmdStep.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgStep, 0, msgStep.Length);
            }

            // Hierdie was vir die Fine Step, sal dit later weer gebruik

            /*
             * int loops = int.Parse(cmbStepLoop.Text);
             * UInt16 stepSize = UInt16.Parse(cmbStepSize.Text); //1800;
             * UInt16 current = UInt16.Parse(cmbStepCurrent.Text); //500;
             *
             * Messages.Msg_Command cmd;
             * if (cmbStepDirection.Text == "CCW")
             *      cmd = new Messages.Msg_Command
             *      {
             *              command = Messages.COMMAND_TYPES.STEP_FORWARD,
             *              param1 = (byte)(stepSize >> 8),
             *              param2 = (byte)(stepSize & 0x00FF),
             *              param3 = (byte)(current >> 8),
             *              param4 = (byte)(current & 0x00FF)
             *      };
             * else
             *      cmd = new Messages.Msg_Command
             *      {
             *              command = Messages.COMMAND_TYPES.STEP_BACK,
             *              param1 = (byte)(stepSize >> 8),
             *              param2 = (byte)(stepSize & 0x00FF),
             *              param3 = (byte)(current >> 8),
             *              param4 = (byte)(current & 0x00FF)
             *      };
             *
             * byte[] msg = serialMessages.GeneratePacket(cmd.Serialize());
             *
             * for (int i = 0; i < loops; i++)
             * {
             *      if (sPort.IsOpen)
             *              sPort.Write(msg, 0, msg.Length);
             *
             *      System.Threading.Thread.Sleep(10);
             * }
             */
        }
Пример #3
0
        private void btnFirmwareUpload_Click(object sender, EventArgs e)
        {
            //FlashLoader flashProg = new FlashLoader();

            // Step 1: Place closed loop drive into UART bootloader mode
            Messages.Msg_Command cmdJumpToBootloader;

            cmdJumpToBootloader = new Messages.Msg_Command {
                command = Messages.COMMAND_TYPES.JUMP_BOOTLOADER
            };

            byte[] msgJumpToBootloader = serialMessages.GeneratePacket(cmdJumpToBootloader.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgJumpToBootloader, 0, msgJumpToBootloader.Length);
            }

            // Close the port to make it available for the flashloader
            sPort.Close();
            while (sPort.IsOpen)
            {
                ;
            }

            System.Threading.Thread.Sleep(1000);


            // Step 2: Use the STMFlashloader program to flash the selected file
            string arguments = "-c " +
                               " --pn " + cmbPortName.Text.Replace("COM", "") +
                               " --br 115200 --db 8 --pr EVEN --sb 1 --ec OFF --to 10000 --co ON -Dtr --Hi -Rts --Lo" +
                               " -i STM32F0_5x_3x_64K" +
                               " -u --fn " + txtFirmwareUploadPath.Text +
                               " -Dtr --Lo -Rts --Hi --Lo";


            //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\STMicroelectronics\Software\Flash Loader Demo\STMFlashLoader.exe", arguments);

            // Console.WriteLine(flashProg.Connect());
            // Console.WriteLine(flashProg.Close());


            System.Diagnostics.ProcessStartInfo processFlashLoader = new System.Diagnostics.ProcessStartInfo();
            processFlashLoader.FileName  = @"C:\Program Files (x86)\STMicroelectronics\Software\Flash Loader Demo\STMFlashLoader.exe";
            processFlashLoader.Arguments = arguments;
            processFlashLoader.RedirectStandardOutput = true;
            processFlashLoader.UseShellExecute        = false;

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.OutputDataReceived += (sende, args) => Console.WriteLine("FlashLoader: {0}", args.Data);

            p.StartInfo = processFlashLoader; //System.Diagnostics.Process.Start(processFlashLoader);
            p.Start();
            p.BeginOutputReadLine();
        }
Пример #4
0
        private void btnFirmwareDownload_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFirmwareFile = new SaveFileDialog();

            saveFirmwareFile.Filter = "Bin files|*.bin|Hex files|*.hex|All files|*.*";

            if (saveFirmwareFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string saveFileName = saveFirmwareFile.FileName;

            // Step 1: Place closed loop drive into UART bootloader mode
            Messages.Msg_Command cmdJumpToBootloader;

            cmdJumpToBootloader = new Messages.Msg_Command {
                command = Messages.COMMAND_TYPES.JUMP_BOOTLOADER
            };

            byte[] msgJumpToBootloader = serialMessages.GeneratePacket(cmdJumpToBootloader.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgJumpToBootloader, 0, msgJumpToBootloader.Length);
            }

            // Close the port to make it available for the flashloader
            sPort.Close();
            while (sPort.IsOpen)
            {
                ;
            }

            System.Threading.Thread.Sleep(1000);


            // Step 2: Use the STMFlashloader program to download to the selected file
            string arguments = "-c " +
                               " --pn " + cmbPortName.Text.Replace("COM", "") +
                               " --br 115200 --db 8 --pr EVEN --sb 1 --ec OFF --to 10000 --co ON -Dtr --Hi -Rts --Lo" +
                               " -i STM32F0_5x_3x_64K" +
                               " -u --fn " + saveFileName +
                               " -Dtr --Lo -Rts --Hi --Lo";


            System.Diagnostics.ProcessStartInfo processFlashLoader = new System.Diagnostics.ProcessStartInfo();
            processFlashLoader.FileName        = @"C:\Program Files (x86)\STMicroelectronics\Software\Flash Loader Demo\STMFlashLoader.exe";
            processFlashLoader.Arguments       = arguments;
            processFlashLoader.UseShellExecute = true;

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = processFlashLoader;
            p.Start();
        }
Пример #5
0
        private void BtnBootloaderRun_Click(object sender, EventArgs e)
        {
            Messages.Msg_Command cmdJumpToBootloader = new Messages.Msg_Command {
                command = Messages.COMMAND_TYPES.JUMP_BOOTLOADER
            };
            byte[] msgJumpToBootloader = serialMessages.GeneratePacket(cmdJumpToBootloader.Serialize());
            if (sPort.IsOpen)
            {
                sPort.Write(msgJumpToBootloader, 0, msgJumpToBootloader.Length);
            }

            BtnConnect_Click(sender, e);
            MessageBox.Show("Now you device in a bootloader mode", "Bootloader");
        }
Пример #6
0
        private void chkTuningEnable_CheckedChanged(object sender, EventArgs e)
        {
            if (!sPort.IsOpen)
            {
                return;
            }

            Messages.Msg_Command cmdStreamAngle;

            if (chkTuningEnable.Checked)
            {
                cmdStreamAngle = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.STREAM_ANGLE, param1 = 1
                };
                ThreadHelperClass.SetEnable(this, btnAutoTune, false);

                pidAutotuneClass.Clear();

                serialMessages.MessageReceived += SerialMessages_MessageReceived1;
                timerRenderScottPlot            = new System.Threading.Timer(TimerRender, null, 5, 5);
            }
            else
            {
                cmdStreamAngle = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.STREAM_ANGLE, param1 = 0
                };

                serialMessages.MessageReceived -= SerialMessages_MessageReceived1;
                ThreadHelperClass.SetEnable(this, btnAutoTune, true);
                ThreadHelperClass.SetNumericUpDownValue(this, numMaxAngleError, (decimal)pidAutotuneClass.GetMaxAngleError);
                ThreadHelperClass.SetNumericUpDownValue(this, numAvgAngleError, (decimal)pidAutotuneClass.GetAvgAngleError);

                timerRenderScottPlot.Dispose();
                //timerRenderScottPlot.Change(Timeout.Infinite, Timeout.Infinite);
            }

            byte[] msgStreamAng = serialMessages.GeneratePacket(cmdStreamAngle.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgStreamAng, 0, msgStreamAng.Length);
            }
        }
Пример #7
0
        private void rdClosedLoop_CheckedChanged(object sender, EventArgs e)
        {
            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            if (rdClosedLoop.Checked)
            {
                Messages.Msg_Command cmdModeClose = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.MODE_CLOSELOOP
                };
                byte[] msgModeClose = serialMessages.GeneratePacket(cmdModeClose.Serialize());
                if (sPort.IsOpen)
                {
                    sPort.Write(msgModeClose, 0, msgModeClose.Length);
                }
            }
        }
Пример #8
0
        private void chkEnable_CheckedChanged(object sender, EventArgs e)
        {
            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            if (chkEnable.Checked)
            {
                //Verify the mode before enabling
                rdOpenLoop_CheckedChanged(this, e);
                rdClosedLoop_CheckedChanged(this, e);

                // Seem unreliable to write while enabled
                btnWriteParameters.Enabled = false;

                Messages.Msg_Command cmdModeEnable = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.MODE_ENABLE
                };
                byte[] msgModeEnable = serialMessages.GeneratePacket(cmdModeEnable.Serialize());
                if (sPort.IsOpen)
                {
                    sPort.Write(msgModeEnable, 0, msgModeEnable.Length);
                }
            }
            else
            {
                btnWriteParameters.Enabled = true;

                Messages.Msg_Command cmdModeDisable = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.MODE_DISABLE
                };
                byte[] msgModeDisable = serialMessages.GeneratePacket(cmdModeDisable.Serialize());
                if (sPort.IsOpen)
                {
                    sPort.Write(msgModeDisable, 0, msgModeDisable.Length);
                }
            }
        }
Пример #9
0
        private void chkTuningEnable_CheckedChanged(object sender, EventArgs e)
        {
            Messages.Msg_Command cmdStreamAngle;

            if (chkTuningEnable.Checked)
            {
                cmdStreamAngle = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.STREAM_ANGLE, param1 = 1
                };
            }
            else
            {
                cmdStreamAngle = new Messages.Msg_Command {
                    command = Messages.COMMAND_TYPES.STREAM_ANGLE, param1 = 0
                };
            }

            byte[] msgStreamAng = serialMessages.GeneratePacket(cmdStreamAngle.Serialize());

            if (sPort.IsOpen)
            {
                sPort.Write(msgStreamAng, 0, msgStreamAng.Length);
            }
        }
Пример #10
0
        private void btnWriteParameters_Click(object sender, EventArgs e)
        {
            // Check if serial protocol object exist
            if (serialMessages == null)
            {
                return;
            }

            try
            {
                Int16 valKp       = Int16.Parse(driverParameters.Items.First(o => o.Name == "Kp").ValueStr);
                Int16 valKi       = Int16.Parse(driverParameters.Items.First(o => o.Name == "Ki").ValueStr);
                Int16 valKd       = Int16.Parse(driverParameters.Items.First(o => o.Name == "Kd").ValueStr);
                Int16 valCurrent  = (Int16)Math.Ceiling(Int16.Parse(driverParameters.Items.First(o => o.Name == "Current").ValueStr) / 6.5);
                Int16 valStepSize = Int16.Parse(driverParameters.Items.First(o => o.Name == "Step Size").ValueStr);
                Int16 valENDir    = Int16.Parse(driverParameters.Items.First(o => o.Name == "EN Pin").ValueStr);
                Int16 valMotorDir = Int16.Parse(driverParameters.Items.First(o => o.Name == "Motor Direction").ValueStr);

                Messages.Msg_SetParam setParamKp = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.KP, value = valKp
                };
                Messages.Msg_SetParam setParamKi = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.KI, value = valKi
                };
                Messages.Msg_SetParam setParamKd = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.KD, value = valKd
                };
                Messages.Msg_SetParam setParamCurrent = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.CURRENT, value = valCurrent
                };
                Messages.Msg_SetParam setParamStepSize = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.STEPSIZE, value = valStepSize
                };
                Messages.Msg_SetParam setParamENDir = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.ENDIR, value = valENDir
                };
                Messages.Msg_SetParam setParamMotorDir = new Messages.Msg_SetParam {
                    param = Messages.PARAMETER_TYPES.MOTORDIR, value = valMotorDir
                };

                byte[] msgKp       = serialMessages.GeneratePacket(setParamKp.Serialize());
                byte[] msgKi       = serialMessages.GeneratePacket(setParamKi.Serialize());
                byte[] msgKd       = serialMessages.GeneratePacket(setParamKd.Serialize());
                byte[] msgCurrent  = serialMessages.GeneratePacket(setParamCurrent.Serialize());
                byte[] msgStepSize = serialMessages.GeneratePacket(setParamStepSize.Serialize());
                byte[] msgENDir    = serialMessages.GeneratePacket(setParamENDir.Serialize());
                byte[] msgMotorDir = serialMessages.GeneratePacket(setParamMotorDir.Serialize());

                if (sPort.IsOpen)
                {
                    // Short delays here are for compatibility with BTT serial protocol, which is rather slow
                    sPort.Write(msgKp, 0, msgKp.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgKi, 0, msgKi.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgKd, 0, msgKd.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgCurrent, 0, msgCurrent.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgStepSize, 0, msgStepSize.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgENDir, 0, msgENDir.Length);
                    Thread.Sleep(100);

                    sPort.Write(msgMotorDir, 0, msgMotorDir.Length);
                    Thread.Sleep(100);

                    if (chkWriteToFlash.Enabled & chkWriteToFlash.Checked)
                    {
                        Messages.Msg_Command cmdFlashSave = new Messages.Msg_Command {
                            command = Messages.COMMAND_TYPES.STORAGE_SAVE
                        };
                        byte[] msgFlashSave = serialMessages.GeneratePacket(cmdFlashSave.Serialize());
                        sPort.Write(msgFlashSave, 0, msgFlashSave.Length);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Something went wrong while trying to write parameters!", "Writing Error");
            }
        }