Serial Communication with BBB

292 views
Skip to first unread message

Dror Lugasi

unread,
Jul 31, 2016, 7:17:41 AM7/31/16
to BeagleBoard
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! 


Wally Bkg

unread,
Jul 31, 2016, 12:08:11 PM7/31/16
to BeagleBoard
Good place to start for serial programming in C

I've no experience with CAN ports.

Dennis Lee Bieber

unread,
Jul 31, 2016, 12:30:17 PM7/31/16
to beagl...@googlegroups.com
On Sun, 31 Jul 2016 04:17:41 -0700 (PDT), Dror Lugasi
<lugas...@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.


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

TJF

unread,
Jul 31, 2016, 12:37:52 PM7/31/16
to BeagleBoard
Hi Dror!

Serial communication is no issue. Just load the overlay to configure the pins. Then configure the port, open the port, and read or write data.

The point is "controlling motor drivers". What kind of control do you need (just on/off, or complex power control, may be in closed loop control at high frequency with QEP sensor).

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 need low latency. Prefer compiler languages, and use the one you're most comfortable with.

BR

William Hermans

unread,
Jul 31, 2016, 3:37:54 PM7/31/16
to beagl...@googlegroups.com
The code examples are more or less code "ramblings" as I was learning the socketCAN framework, while reverse engineering a custom J1939 CAN protocol.

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/b22c1df1-fedb-47b6-aa11-13e70e545775%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dennis Lee Bieber

unread,
Jul 31, 2016, 10:15:38 PM7/31/16
to beagl...@googlegroups.com
On Sun, 31 Jul 2016 09:37:52 -0700 (PDT), TJF
<jeli.f...@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.

jackson...@gmail.com

unread,
Aug 1, 2016, 9:50:08 AM8/1/16
to BeagleBoard
thanks! i'll take a look! :) 
is there a way to program and compile it in windows and than take the executable and use it in the beaglebone?

jackson...@gmail.com

unread,
Aug 1, 2016, 9:50:08 AM8/1/16
to BeagleBoard
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! :) you are very helpful!

William Hermans

unread,
Aug 1, 2016, 3:40:00 PM8/1/16
to beagl...@googlegroups.com
On Sun, Jul 31, 2016 at 10:05 AM, <jackson...@gmail.com> wrote:
thanks! i'll take a look! :) 
is there a way to program and compile it in windows and than take the executable and use it in the beaglebone?

The short answer is no.

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.

Dennis Lee Bieber

unread,
Aug 1, 2016, 8:20:09 PM8/1/16
to beagl...@googlegroups.com
On Sun, 31 Jul 2016 10:32:18 -0700 (PDT),
jackson...@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:
http://unix.stackexchange.com/questions/156708/does-the-timeslice-depend-on-process-priority-or-not-under-completely-fair-sched

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: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

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).

William Hermans

unread,
Aug 1, 2016, 8:44:51 PM8/1/16
to beagl...@googlegroups.com
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.

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.

woody stanford

unread,
Jan 29, 2017, 8:59:40 PM1/29/17
to BeagleBoard
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?

woody stanford

unread,
Jan 29, 2017, 9:15:29 PM1/29/17
to BeagleBoard
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.


On Sunday, July 31, 2016 at 4:17:41 AM UTC-7, Dror Lugasi wrote:
Reply all
Reply to author
Forward
0 new messages