So I found my Basic Stamp, a BS2, that came with a Boe-Bot I purchased several years ago. I started looking for my old boe-bot code and thought I could setup a serial communication from a C# application to the chip. Here’s what I came up with:
' {$STAMP BS2}
' {$PBASIC 2.5}
'page 253 for ir hookup
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T1200 CON 813
T2400 CON 396
T9600 CON 84
T19K2 CON 32
T38K4 CON 6
#ENDSELECT
Inverted CON $4000
Open CON $8000
Baud CON T9600 + Inverted
command VAR Byte
steps VAR Word
counter VAR Word
motor VAR Byte
speed VAR Byte
DEBUG "running"
DO
PULSOUT 12, 750
PULSOUT 13, 750
PULSOUT 14, 750
PULSOUT 15, 750
SERIN 16, Baud,[command]
SELECT command
CASE "m"
GOSUB motion
CASE ELSE
GOSUB default
ENDSELECT
PAUSE 20
LOOP
default:
SEROUT 16, Baud, ["n",0]
RETURN
motion:
' m motor speed steps
SERIN 16, Baud, [motor]
DEBUG motor
SERIN 16, Baud, [speed]
DEBUG speed
SERIN 16, Baud, [steps]
DEBUG steps
FOR counter = 1 TO steps
PULSOUT 12+motor, 650+speed
PAUSE 20
NEXT
RETURN
And with that code setup on the bot, I used this nice function in C# to send the orders to the servos connected to the bot.
private static SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
private static Queue toSend = new Queue();
private static void Main()
{
Console.WriteLine("Waiting for data...");
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Thread reader = new Thread(Read);
reader.IsBackground = true;
reader.Start();
motion(0,100,10); // run first servo counter-clockwise at full speed for 10 seconds
}
static void motion(byte motor, int speed, int seconds)
{
if (speed < -100) { speed = -100; } else if (speed > 100)
{
speed = 100;
}
double loop = (1.5 + 20 + 1.6) / 1000;
int pulses = (int)Math.Round(seconds / loop, 0);
lock (toSend)
{
toSend.Enqueue(Convert.ToByte('m'));
toSend.Enqueue(motor);
toSend.Enqueue((byte)(speed+100));
foreach (byte b in BitConverter.GetBytes(pulses))
toSend.Enqueue(b);
}
}
static void Read()
{
while (true)
{
if (toSend.Count > 0)
{
byte var = new byte();
lock (toSend)
{
var = toSend.Dequeue();
}
port.BaseStream.WriteByte(var);
}
Thread.Sleep(10);
}
}