Hi

I won't bother repeating what Charles and Michael already said,
usleep, read, write etc, all userspace.

Attached is a conversion of your comp to a userspace format.
comp quite happily builds userspace components from C.

It builds, but obviously I cannot test it.

The initial usleep in the loop may not be required, it can be dispensed with or adjusted as required

regards


On 01/07/16 15:27, matthew venn wrote:
Thanks Charles.
I started off with a non realtime python program loaded using loadusr, but it seemed too jittery to me.
I'll have another go and post back.

Matt

On Friday, 1 July 2016 15:56:32 UTC+2, Charles Steinkuehler wrote:
On 7/1/2016 8:01 AM, matthew venn wrote:
> Hello,
>
> I'm working on a bipod (polargraph) style drawing robot. I'm using machinekit on
> beaglebone.
>
> I'm moving the pen up and down with a radio (xbee) controlled servo. I've
> written a hal component in C that opens (non blocking) a serial port on the
> beaglebone and then reads and writes to it. The component takes just over 25ms
> to run.
> This component is added to a slow thread (50ms). I don't get realtime error
> messages or joint following errors.

You should add something that takes that long to run to a
non-real-time thread.  Even in a slow RT thread, your code is running
with real-time priority which keeps other parts of the system from
getting CPU cycles.

Just write a normal program that does what you need and use "loadusr"
instead of "loadrt".  You can even craft code in python for talking to
slow things like serial ports.  See the python temperature reading
code (in the various 3D printer configs) for an example:

https://github.com/machinekit/machinekit/blob/master/src/hal/user_comps/hal_temp_bbb.py

If you really want to use a real-time HAL component for this, you need
to code the logic so each pass through the function happens quickly.
That means keeping track of your current state in variables that
survive across calls to your function, and when your function does
run, you check to see if you can do the next step (like write the next
byte or read a character).  If so, you do that, otherwise you just
exit.  Also, you need to talk directly to the hardware (or via an
existing HAL driver).  Performing system calls (like reading or
writing to a file descriptor) and doing things like usleep() in a RT
function are very bad.  The system calls will break all real-time
guarantees for HAL, since the Linux kernel can do anything (like page
a bunch of memory out to swap) once you give it control, and your
usleep() is just burning CPU cycles at real-time priority.

I strongly suggest you use a user-mode HAL component instead.  The
code you've got would be fine as a user-mode component.

--
Charles Steinkuehler
[email protected]
--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Visit this group at https://groups.google.com/group/machinekit.
For more options, visit https://groups.google.com/d/optout.

--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Visit this group at https://groups.google.com/group/machinekit.
For more options, visit https://groups.google.com/d/optout.
component xbee "xbee serial";
pin in float pos_;
pin out float batt_;
pin in float scale = 20.0;

function _;

option singleton yes;               // makes no sense to have more than one of 
these components running
option userspace yes;

license "GPL"; // indicates GPL v2 or later
;;

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>

int fd;

struct {
    char pos;
    char cksum;
} tx;

struct {
    unsigned short batt;
    unsigned char cksum;
} rx;

#define RX_SIZE 3 //computed as 4 but on the atmel it's 3

int serialport_init();
char CRC8(char *data, char len);

void user_mainloop(void)
{
char buffer[50];
char ch;

    int ret = serialport_init();
    
    while(ret)
        {
        usleep(100000);
        FOR_ALL_INSTS()  
            { 
            tx.pos = pos_ * scale;
            char buf[sizeof(tx)];
            memcpy(&buf, &tx, sizeof(tx));
            tx.cksum = CRC8(buf,sizeof(tx)-1);
            memcpy(&buf, &tx, sizeof(tx));

            write (fd, buf, sizeof(tx));
            usleep (35 * 1000); // sleep enough time for the chars to get sent

            char rx_buf[RX_SIZE];
            int n = read (fd, rx_buf, RX_SIZE);

            //copy buffer to structure
            memcpy(&rx, &rx_buf, RX_SIZE);
            if(rx.cksum != CRC8(rx_buf,RX_SIZE-1))
                batt_ = -1;
            else
                batt_ = rx.batt;
            }           
        }

    close(fd);
    exit(0);
}

//######################################################################

int serialport_init()
{
    if ((fd = open("/dev/ttyO1", O_RDWR | O_NONBLOCK))<0)
    {
        printf("UART: Failed to open the file.\n");
        return 0;
    }

    struct termios options; // the termios structure is vital
    tcgetattr(fd, &options); // sets the parameters associated with file

    // Set up the communications options:
    // 57600 baud, 8-bit, enable receiver, no modem control lines
    options.c_cflag = B57600 | CS8 | CREAD | CLOCAL;
    tcflush(fd, TCIFLUSH); // discard file information not transmitted
    tcsetattr(fd, TCSANOW, &options); // changes occur immmediately
    return 1;
}

char CRC8(char *data, char len) 
{
    char crc = 0x00;
    while (len--)
    {
        char extract = *data++;
        char tempI;
        for (tempI = 8; tempI; tempI--) 
        {
            char sum = (crc ^ extract) & 0x01;
            crc >>= 1;
            if(sum) 
            {
                crc ^= 0x8C;
            }
            extract >>= 1;
        }
    }
    return crc;
}

Reply via email to