HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Opinion on performance increase of a robot

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
opinionincreaseperformancerobot

Problem

Background

A prototype of a firefighting robot has been done recently based on an Arduino microcontroller. It has two servos, one laser LED and 2WD motors. The desktop application is done in C# to control the robot.

Desktop app logic

For controlling two servos, I am using the x-y mouse position on the screen and bind keys from keyboard for controlling the motors. Finally, values of servos, led and motors are combined into a single string like "x140y090s0we" - "x140" - servo1 140degree, "y090" - servo2 90 degree, "s0" - laser is off, "w" - for motors, "e" - end of command.

On the microcontroller side

A loop gathers each character one by one into an array. Then it recognizes appropriate values from [x,1,4,0,y,0,9,0,s,0,w,e] to manipulate some parts of the robot.

Now the problem

The robot works perfect on a 9600 baud rate, when it is connected through a USB cable port. However, when trying to manipulate it using a BLUETOOTH port, lags start to appear. I solved the problem by increasing the baud rate of the HC-06 BLUETOOTH module to 115200, so everything works fine. However, power supply consumption increased as well.

Question

Honestly saying, I don't have much experience in programming. I tried my best. How would you suggest to improve the code so that it shows best performance in 9600 baud rate using bluetooth module?

C# Code:

```
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
init();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;

}
private void init()
{
port = new SerialPort();
port.PortName = "COM5";
port.BaudRate = 115200;

try
{
port.Open();
}
catch (Exception e1)
{

MessageBox.Show(e1.Message);
}
}

Solution

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.W)
        input = "r";
    if (e.KeyCode == Keys.S)
        input = "l";
    if (e.KeyCode == Keys.A)
        input = "b";
    if (e.KeyCode == Keys.D)
        input = "f";
    Display(input);
}


...

public void Display(string inp)
{
    if (inp == "f")
        label4.Text = "Right";
    if (inp == "b")
        label4.Text = "Left";
    if (inp == "l")
        label4.Text = "Back";
    if (inp == "r")
        label4.Text = "Forward";
}


Each of these methods will continue to evaluate the next if statement(s) even if one returns true; e.g. in Form1_KeyDown, if the key is in fact Keys.W, input will be set to "r". However it then goes on to check whether the Key is equal to S, A, and D, which is unnecessary and inefficient. Same for the other method.
Use else if, or a switch statement.

Code Snippets

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.W)
        input = "r";
    if (e.KeyCode == Keys.S)
        input = "l";
    if (e.KeyCode == Keys.A)
        input = "b";
    if (e.KeyCode == Keys.D)
        input = "f";
    Display(input);
}
public void Display(string inp)
{
    if (inp == "f")
        label4.Text = "Right";
    if (inp == "b")
        label4.Text = "Left";
    if (inp == "l")
        label4.Text = "Back";
    if (inp == "r")
        label4.Text = "Forward";
}

Context

StackExchange Code Review Q#122277, answer score: 5

Revisions (0)

No revisions yet.