Serial Communication with BBB

hey guys,
i am new to BBB and i need to use it for serial communication: reading data from IMU and controlling motor drivers according to the readings.

what is the best (easiest and fastest) way to do this?

the beagle is running as a stand alone PC with screen and a keyboard and mouse.
i haven’t got any experience with coding serial communication, i just know the basics, so i am also looking for a good source to read about it.
is the Cloud9 that comes with it good for this application? and in what language is it best? python? js? or maybe c++?
a reference with explanation on how to receive and send data via the UART and CAN ports is the best solution that i am after.

the UART1,2,4 are already configured in ttyO1/2/4. but i wasn’t able to initialize the CAN ports.

thanks for helping!

Good place to start for serial programming in C
https://www.cmrr.umn.edu/~strupp/serial.html

I’ve no experience with CAN ports.

On Sun, 31 Jul 2016 04:17:41 -0700 (PDT), Dror Lugasi
<lugasi.dror@gmail.com> declaimed the
following:

i haven't got any experience with coding serial communication, i just know
the basics, so i am also looking for a good source to read about it.

  Regrettably my old books can't be recommended... "Serial Communications
Developers Guide" is so stale it covers MS-DOS and 16-bit Windows,
eventually getting up to W9x level. "Serial Port Complete" is newer, but
again is Windows based (.NET).

is the Cloud9 that comes with it good for this application? and in what
language is it best? python? js? or maybe c++?

  Cloud9, as I recall, is nothing more than a web-based development
environment (ie: an web-based editor, file manager, and way to execute
programs).

  Heavily biased for the node.js variant of JavaScript, but also allows
access to Python, and a command-line.

a reference with explanation on how to receive and send data via the UART
and CAN ports is the best solution that i am after.

  As for regular serial I/O -- depends upon what you need to do with it.
And what responsiveness you need.

  Text strings or binary data... Fixed length packets or variable length
with start/end markers (ie: what protocols are in use -- with proper
drivers one could run SLIP even). High data rate constant stream <> high
rate bursts with gaps between them <> low-rate stream... Can the OS handle
buffering while your application uses standard C-library type read/write
calls? Can you block for data to arrive, or do you need to be notified of
every byte transferred.

Hi Dror!

thanks! i’ll take a look! :slight_smile:
is there a way to program and compile it in windows and than take the executable and use it in the beaglebone?

Wally Bkg:

thanks! i’ll look it up.

Dennis Lee Bieber:

thanks! i have managed to establish a serial connection with the PC via RS-232,
the code was written in cloud 9 in Python, it was fairly easy and didn’t took too long.
my goal is to read serial data from an IMU that sends packets of data in a high speed and make stabilization program for 2 motors.
it doesn’t need it to be super fast, it needs to update at around 20-30 times a second tops.

TJF:
thanks for the tip! i already know the basics of serial communication but i haven’t used in in an actual system.
so for the needs i specified for Dennis do you think that python will be ok? or should i use c++?
it’s just that in python it’s super easy and in C everything is over complicated, especially for a beginner like me.
what do you recommend i should do? learning to program serial in C might be hard and annoying compared to python.

thanks for everybody for answering! :slight_smile: you are very helpful!

Setting up canutils: http://forum.43oh.com/topic/8772-setting-up-canutils-on-linux/

code examples: http://forum.43oh.com/topic/8541-socketcan-help-needed/

The code examples are more or less code “ramblings” as I was learning the socketCAN framework, while reverse engineering a custom J1939 CAN protocol.

On Sun, 31 Jul 2016 09:37:52 -0700 (PDT), TJF
<jeli.freiherr@gmail.com> declaimed the
following:

For a closed loop controller I wouldn't use interpreter languages (Phyton,
JS). The interpreter may do a garbage collection in that moment when you

  For the most part, the common Python uses ref-counting, so objects are
collected at the point where the count of references goes to zero; so there
normally isn't a "random" mark/free scan... Though one can be invoked
manually.

Technically it is possible, but it would require too much work to setup,
and the result is not really what you'd want most likely. It's better to
create a virtual machine, run Linux on it, and create / use virtual
peripherals to write your code. Then simply port the code over to the
beaglebone, which would mostly likely mean moving the code to the
beaglebone, and compiling it natively.

On Sun, 31 Jul 2016 10:32:18 -0700 (PDT),
jacksonist.lugi@gmail.com declaimed the
following:

my goal is to read serial data from an IMU that sends packets of data in a
high speed and make stabilization program for 2 motors.
it doesn't need it to be super fast, it needs to update at around 20-30
times a second tops.

  You haven't specified the size of said packets, but at 20Hz, you've got
a full 50mSec between updates. For comparison, that is over two normal
time-slices for Windows 7 processes. The Linux scheduler seems to adjust
time-slices based on load and "nice" value:

  Either way, if the process is blocking on I/O requests, there's
probably enough CPU available to handle it. At high speed serial
(115200bps, 8N1), you are looking at 5760 bits in a 50mSec window, or 576
bytes [start, 8bits, stop => 10 bits/byte]. I suspect your data is probably
closer to 50 bytes, so call it 5mSec to read... That gives you [using Win7
value] another 15mSec to process the data and generate output before
getting swapped out by the scheduler. Of course, if you are the only
process really doing anything, the odds are that the scheduler will just
bring you back into running state <G>

  Since the low-level I/O, even from Python, is rather optimized (C
runtime library), I doubt it will be a concern.

def looper():

... strt = time.time()
... endt = strt + 0.050
... l = 0
... while time.time() < endt:
... l += 1
... print "Loops completed: %s" % l
...

looper()

Loops completed: 413376

That's on a 3.4GHz Win7 box.

  Hmmm... the BBB seems a bit slower than I'd expected -- or time.time()
is returning greatly different units between Windows and the BBB

login as: debian
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2015-11-12

Support/FAQ: Beagleboard:BeagleBoneBlack Debian - eLinux.org

default username:password is [debian:temppwd]

debian@beaglebone:~$ vim looper.py
debian@beaglebone:~$ python looper.py
Loops completed: 4131

  Only 1% of the speed on the Win7 box?!

Seems consistent though... Adding some arithmetic...

def looper():

... strt = time.time()
... endt = strt + 0.050
... l = 0
... while time.time() < endt:
... l += 1
... _ = l * l
... print "Loops completed: %s" % l
...

looper()

Loops completed: 219002

vs

debian@beaglebone:~$ vim looper.py
debian@beaglebone:~$ python looper.py
Loops completed: 2406

  I'd been thinking maybe 25% (1GHz 32-bit vs 3.4GHz 64-bit)... Floating
point may be killing it, since I don't think the BBB has double precision
hardware (could be wrong).

I agree with TJF,

It’s best not to use interpreted languages( scripting languages ) if you want reasonably deterministic behavior. In fact, this might be a job better suited for a PRU. Not because this task would require a lot of speed, but because when I think of motor stability, I think of deterministic code.

The only way your code will be guaranteed to be deterministic. Would be to run it on a PRU.

If you do however feel that a PRU is unnecessary. A compiled language such as C should be used. Hell, use golang if it makes you feel good. So long as the language is not interpreted.

Best and fastest? Yes, yes.

Use C, namely GCC. We are men here.

OK, what you do is do it in hardware. Select a nice set of glue translating your TTL to 3.3v which the BBB uses. Use SSR chips or just plain logic chips to do this.

Next you’ll be implementing quite a bit of hardware so get some presensitized board and ferric chloride and some cheap Chinese breadboards for designing. etc. etc. Mouser or Digikey is your new best friend. Connect your IMU via the glue to the UARTs on your BBB. Set it up bidirectional as you will need to configure your IMU (ie. both TX and RX). Set your sampling at about 10 to 100 hz with an “attitude engine” reading on your IMU (x,y,z,vx,vy,vz) not the raw signals (that’s so 2000’s).

OK, motor control. Real simple. PWM aka pulse width modulation. What you want to do is break out your PWM to a slave MCU (via BBB’s UARTS) like a PIC16Fx and use software-implemented, crystal controlled PWM on your MCU. Then you 2 or 3 stage it (SSR, logic or driver) with quick circuitry to your MOSFETs (don’t forget the backward diode or you’ll damage your FETs and remember that FET’s usually ground out the back so make sure you use a separate heat sink for each FET, some caps in there somewhere might be nice too).

Then use the BBB to crunch the numbers. Basically this set up transfers your underlying algorithm to a programming domain from a hardware one.

What are you building btw? A drone?

OK, the civilized way to do this stuff. It’s called 0.1" header, ok? You do the traces on single-sided presensitized DIY PCB board and you solder the header on the down side. Then you have all your p8 and p9 signals broken out. Then the whole issue about where do I put all this stuff goes away…you just mount it and connect it on the board.

You just pop in your BBB upside down using your headers as the mount. Then case it.

Voila.