go_away

Author Topic: Writing C code to interface with Axon  (Read 1329 times)

0 Members and 1 Guest are viewing this topic.

Offline mstachoTopic starter

  • Supreme Robot
  • *****
  • Posts: 364
  • Helpful? 10
Writing C code to interface with Axon
« on: January 18, 2011, 08:26:06 AM »
I need more control over my axon.  Ultimately it is going to act as a controller of my motors, but all the higher level computation will take place on a central computer.  Can anyone point me in the direction of a website or a book that would explain how to write C-code (or C#, or java, it doesn't matter) that can allow me to send data to something like an axon?

I can't use hyperterminal since the data I'm sending will be the result of calculations and commands...

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: Writing C code to interface with Axon
« Reply #1 on: January 18, 2011, 09:16:42 AM »
It depends on what you need done.

For example, Visual Basic allows you to send and receive data on a COM port. A google search will bring up example code.

The same for other languages.

For example:
http://www.devasp.net/net/articles/display/727.html

And an example google search to help you:
http://www.google.com/search?&q=receive+data+from+com+port+C

(where C is the language you want)

Offline mstachoTopic starter

  • Supreme Robot
  • *****
  • Posts: 364
  • Helpful? 10
Re: Writing C code to interface with Axon
« Reply #2 on: January 18, 2011, 09:25:22 AM »
Thanks.  I'll keep googling :-) Maybe if I get it working I'll post some code and a quick tutorial.

(Sorry if this gets posted a few times, a weird internet problem happened when trying to post)

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline mstachoTopic starter

  • Supreme Robot
  • *****
  • Posts: 364
  • Helpful? 10
Re: Writing C code to interface with Axon
« Reply #3 on: January 20, 2011, 11:49:20 AM »
Alrighty, so I got *something* working.  It's not the greatest, since I can't seem to control the exact moment that data gets sent to the axon, but for my purposes it works for now.  I'm submitting the code for the community to play with.  I did it in C#.  I'm submitting both the C# code and the Axon control.c program. 

These programs might be good starting points, so if anyone wants to make them work *way* better than what I did, by all means :-)

So the C# code is:
Code: [Select]
using System;
using System.IO.Ports; //Where you find SerialPort
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //So, as a console application, this goes like this
            SerialPort serialPort1 = new SerialPort();

            //Have to initialize on my own here
            serialPort1.BaudRate = 115200;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.PortName = "COM3"; //My COM port is COM3.  Others may have different COM ports.

            if (serialPort1.IsOpen)
                serialPort1.Close();

            //Open and write something
            serialPort1.Open();

            int lengthZ = 4;
            Byte[] z = new Byte[lengthZ];
            Byte[] ST = new Byte[2];

            //In all honesty, these codes are completely arbitrary, and if I were doing this
            //properly I'd avoid the magic numbers...
            /*
             * They work like this:  PC sends 40.  uC receives 40 and sends 39 ("SEND DATA").  Upon receipt
             * of 39, PC sends the data word, which starts with 41 and ends with 42.  When the uC receives
             * 42, it sensd 43 ("DATA RECEIVED").  At the moment the PC just keeps sending, but that's just some coding
             */
            ST[0] = 40; //"I HAVE DATA"
            z[0] = 41; //START byte
            z[1] = 10;
            z[2] = 5;
            z[3] = 42;  //STOP byte
            while (true)
            {
                serialPort1.Write(ST, 0, 1); //start communicating
                if(serialPort1.ReadLine().Contains("39")) //wait until uC is ready
                {
                    for (int i = 0; i < lengthZ; i++)
                    {
                        serialPort1.Write(z, i, 1); //write the data, one Byte at a time
                       
                        if (serialPort1.ReadLine().Contains("43")) //Check if we've received the whole message
                        {
                            Console.WriteLine("OK!");
                        }
                    }
                }
               
            }

           
           
            serialPort1.Close();
            Console.ReadLine(); //Wait so I can see the output before it closes
        }
    }
}

And the Axon code is:

Code: [Select]
/****************************************************************************
*
*   Copyright (c) 2008 [url=http://www.societyofrobots.com]www.societyofrobots.com[/url]
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
****************************************************************************/

//Add your code here

//enter your code in control here
//photovore is just a default sample program and can be deleted
void control(void)
{

//We're now going to experiment with communications...

int response[5]; //Store the "program" in here
int temp = 0;
int delay1 = 0; //used later
int delay2 = 0;
int delay3 = 0;
int counter = 0;
temp = uart1GetByte();
rprintf("\n");
if(temp == 40 || temp == 41) //41 will only be sent once 40 has been received
{
        rprintf("39 \n"); //uC IS GOING TO ACCEPT PC DATA
temp = uart1GetByte();
if(temp == 41)
{
while(1)
{
temp = uart1GetByte();


//42 is STOP, -1 is NO DATA, and 255 just seemed to be
//on the line if nothing else was, so ignore them
if(temp != 42 && temp != -1 && temp != 255)
{
//The data is valid, so put it into the array
response[counter] = temp;

counter = counter + 1;
}
if(counter == 5 || temp == 42) //Don't overshoot the array size limit
{
rprintf("43 \n");
break;
}
rprintf("39 \n");
//necessary so the computer knows it has to keep sending data
}
uartFlushReceiveBuffer(1);

/*
So what happens next is this:  I have two LEDs attached, one to port A6, and
one to port A7.  The PC sends two numbers.  Each is 1/100 of the length of time,
in milliseconds, that the given LED will turn on.  The first number corresponds
to port A6, the second to port A7. 

Ultimately, the while loop below is just a sample of what you can do
with data, it is in no way meant to be useful :-P
*/

while(1)
{

    rprintf("43 \n");

delay1 = response[0];
delay2 = response[1];

if(delay1 > delay2)
{
delay3 = delay1 - delay2;
PORT_ON(PORTA,6);
if(delay2 > 0)
{
PORT_ON(PORTA,7);
delay_ms(delay2*100);
PORT_OFF(PORTA,7);
}
delay_ms(delay3*100);
PORT_OFF(PORTA,6);
}
else if(delay2 > delay1)
{
delay3 = delay2 - delay1;
PORT_ON(PORTA,7);
if(delay1 > 0)
{
PORT_ON(PORTA,6);
delay_ms(delay1*100);
PORT_OFF(PORTA,6);
}
delay_ms(delay3*100);
PORT_OFF(PORTA,7);

}
else
{
//they're equal
PORT_ON(PORTA,6);
PORT_ON(PORTA,7);
delay_ms(delay1*100);
PORT_OFF(PORTA,6);
PORT_OFF(PORTA,7);
}
delay_ms(1000);
break; //Yeah, I know...break is bad, but
//this statement allows me to exit the program
//without turning the axon off.


}
}

}

}


Current project: tactile sensing systems for multifingered robot hands

 


Get Your Ad Here

data_list