Revisiting VB.net and Pbot

Posted: February 2, 2014 in Digital Electronics, Robotics, Tutorials
Tags: , , , , , , , , ,

This is my first post for the year, been busy for a while. The Pbot is an entry level mobile robot learning platform locally available in e-gizmo here in the Philippines. It contains three channel IR collision sensor, 3 Channel line sensor and a dual motor driver. Any arduino compatible platform can be used as the controller. It is a fun mobot platform to play with, can be configured as a line follower, a mazer or a sumobot or even a soccer bot.
Pbot

A quick weekend project to play with this mobot platform is to control it with VB.net thru Bluetooth Serial Port. When playing with these toys, it is important to know whether your battery still have the juice so you can enjoy playing, but the old board (that I have) don’t have a connection for battery feedback. My quick solution is to put a voltage divider (two 100K resistors) between the battery terminals and feed it to the Analog Input 0 of the controller.

Remote

The Arduino program transmits data every second to the computer then waits for a character from the Computer, When a correct character is received, then it tells the motors what to do.

Voila! Vb.net Controlled Pbot with Battery Status Feedback

The code listing for the Arduino

/*
'Pbot Remote Control with VB.net 2010
'Copyright (C) 2014 Richard Myrick T. Arellaga
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see .
*/

int batt = 0;
float voltage = 0;

long prevMillis = 0;
long interval = 1000;
int rx = 0;

const int M1DIR = 8;
const int M1RUN = 9;
const int M2RUN = 10;
const int M2DIR = 11;

void setup()
{
pinMode(M1DIR,OUTPUT);
pinMode(M1RUN,OUTPUT);
pinMode(M2RUN,OUTPUT);
pinMode(M2DIR,OUTPUT);

Serial.begin(9600);

}

void loop()
{
//Run RTC and send battery update every second
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval)
{

prevMillis = currentMillis;
batt = analogRead(0);
voltage = (batt/1023.0)* (2 * readVcc());
Serial.println(voltage);

}

//check serial port character receive
if(Serial.available()){
rx=Serial.read();

if(rx == 'W')
{
digitalWrite(M1DIR,HIGH);
analogWrite(M1RUN,250);
analogWrite(M2RUN,250);
digitalWrite(M2DIR,HIGH);
rx = 0;
}else if(rx == 'Q')
{
digitalWrite(M1DIR,LOW);
analogWrite(M1RUN,0);
analogWrite(M2RUN,0);
digitalWrite(M2DIR,LOW);
rx = 0;
}else if(rx == 'S')
{
digitalWrite(M1DIR,LOW);
analogWrite(M1RUN,250);
analogWrite(M2RUN,250);
digitalWrite(M2DIR,LOW);
rx = 0;
} else if(rx == 'D')
{
digitalWrite(M1DIR,HIGH);
analogWrite(M1RUN,250);
analogWrite(M2RUN,0);
digitalWrite(M2DIR,LOW);
rx = 0;
}
else if(rx == 'A')
{
digitalWrite(M1DIR,LOW);
analogWrite(M1RUN,0);
analogWrite(M2RUN,250);
digitalWrite(M2DIR,HIGH);
rx = 0;
}
}
}

//read actual VCC of arduino for a more accurate adc reading
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV

return result/1000;
}

VB.net Code Listing


'Pbot Remote Control with VB.net 2010
'Copyright (C) 2014 Richard Myrick T. Arellaga
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see .

Imports System.IO.Ports
Imports System.Text

Public Class Form1

Private pendingMsg As New StringBuilder()

Private Sub GetCOMPortList()
Dim i As Integer
Dim foundDifference = False

If cboCOMPorts.Items.Count = SerialPort.GetPortNames().Length Then
For Each s As String In SerialPort.GetPortNames()
If cboCOMPorts.Items(System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)).Equals(s) = False Then
foundDifference = True
End If
Next
Else
foundDifference = True
End If

If foundDifference = False Then
Return
End If

cboCOMPorts.Items.Clear()

For Each s As String In SerialPort.GetPortNames()
cboCOMPorts.Items.Add(s)
Next
cboCOMPorts.SelectedIndex = 0

End Sub

Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Dim completeMsg As String
completeMsg = String.Empty

pendingMsg.Append(SerialPort1.ReadExisting())

If pendingMsg.Length >= 6 Then
completeMsg = pendingMsg.ToString(0, 6)
pendingMsg.Remove(0, 6)
Me.Invoke(New Action(
Sub()
txtRxData.Text = completeMsg
End Sub))
End If

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnDisconnect.Enabled = False
GetCOMPortList()
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

Try
SerialPort1.PortName = cboCOMPorts.Items(cboCOMPorts.SelectedIndex).ToString()
SerialPort1.BaudRate = 9600
SerialPort1.ReadTimeout = 1000
SerialPort1.Open()
btnDisconnect.Enabled = True
btnConnect.Enabled = False
Catch ex As Exception
btnDisconnect.PerformClick()
End Try
End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Catch ex As Exception

End Try
End Sub

Private Sub btnFW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFW.Click
If (SerialPort1.IsOpen()) Then
SerialPort1.Write("W")

End If
End Sub

Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
If (SerialPort1.IsOpen()) Then
SerialPort1.Write("S")

End If
End Sub

Private Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click
If (SerialPort1.IsOpen()) Then
SerialPort1.Write("A")

End If
End Sub

Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click
If (SerialPort1.IsOpen()) Then
SerialPort1.Write("D")

End If
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
If (SerialPort1.IsOpen()) Then
SerialPort1.Write("Q")

End If
End Sub
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s