Skip to content

uart functionality - #140

Merged
knmcguire merged 2 commits into
bitcraze:masterfrom
s9meboeh:uart
Mar 5, 2020
Merged

knmcguire merged 2 commits into
bitcraze:masterfrom
s9meboeh:uart

Conversation

@s9meboeh

Copy link
Copy Markdown
Contributor

Enable CRTP communication via Uart.

@knmcguire knmcguire left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good, but I'm mostly worried about it being too specific for the Rasp PI. But it seems that it can be made generic pretty easily so that users can also read from uart from for instance, a linux computer.

Comment thread cflib/crtp/serialdriver.py Outdated
self.in_queue = queue.Queue()
self.out_queue = queue.Queue(1)

self.ser = serial.Serial('/dev/ttyAMA0', 512000, timeout=1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having the ability to assign this already in line 86, so that users can define this URI from their script directly? This way, this functionality can also be used on other machines as well, and we can test it out on ubuntu.

@s9meboeh s9meboeh Feb 26, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure that I got it right.
You mean that the URI has the form 'serial://<device string>'?
Would this work from a PC (I have never used Uart from my PC, so I do not know if it works the same as for the Raspberry Pi)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably, like serial://dev/ttyUSB5 or something like that for ubuntu.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, Serial() would expect something like COM3, so one way would be to strip of serial:// and put the rest of the URI into the first argument of the constructor. That would mean serial:///dev/ttyUSB5 on Ubuntu (note the triple slash).

Considering the pyserial documentation we could also pass things like serial://rfc2217://....

Looks a bit weird (and I guess is not valid URI), but the other option would be to pass the full CrazyFlie URI to it. As other drivers would not feel responsible, that could also be a solution. The problem is that Serial takes both URIs or file names.

What is your preference @knmcguire?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of thinking about in the direction of how the USB is handled, where you can select either usb://0, 1, 2 .... so then you will do that for serial://0,1,2....

But if this is too much work for this pull request, just let us know and we can make an issue out of this after acceptance. It is just that we don't have a rasp pi lying around that is properly configured to test this out and would be nice to do this from the computer directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the Uri to the form serial://name where the name is the short device name like ttyAMA0 or ttyUSB5.
To see which serial devices are available you can use the command python -m serial.tools.list_ports -v.

Comment thread cflib/crtp/serialdriver.py Outdated

# Check if it is a valid serial URI
uriRe = re.search('^serial://([a-z A-Z 0-9]+)/?([0-9]+)?$', uri)
uriRe = re.search('^serial://pi$', uri)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment here underneath

@s9meboeh
s9meboeh requested a review from knmcguire March 4, 2020 16:01
@knmcguire

Copy link
Copy Markdown
Contributor

Perfect! With the new changes, I managed to make a connection with the Crazyflie directly from my ubuntu 18.04 with the following codelines URI = 'serial://ttyUSB5' when the CFLAGS += -DUART2_LINK_COMM enabled in config.mk in the firmware. So I can confirm this working :)

I will merge this and the other prrt pull request too. We would like to also receive instructions for this in markdown format so we can add this to the documentation, but that can be done seperately.

@BeyersBots

Copy link
Copy Markdown

@s9meboeh : I am curious if you could provide any guidance with getting serial to work with a Roadrunner and Raspberry Pi 3B+? Specifically, what OS are you running on the pi? What I have done so far is update the firmware to include the CFLAGS += -DUART2_LINK_COMM enabled in the config.mk and reflashed the Roadrunner. Made sure I had the latest version of crazyflie-lib-python, enabled the serial port on the Raspberry Pi (disabled shell), and then ran the basiclog.py. It gets to the point of "Connecting to serial://ttyAMA0" and sits there. I'm not sure if there is anything else I need to do in the process. Thanks again for all of your help.

@s9meboeh

Copy link
Copy Markdown
Contributor Author

@gimlisonofgloin
We are running a Raspbian Buster on a Raspberry Pi Zero W.
My guess would be that UART is not set up for serial communication.
For the Pi Zero (and the 3b+ as well I think) by default the Bluetooth module is connected to the PL011 UART (/dev/ttyAMA0).
So here is what we did, that could work for you as well:

  • Enable the mini Uart (/dev/ttyS0) in /boot/config.txt: enable_uart=1
  • Restore ttyAMA0 as primary Uart in /boot/config.txt: dtoverlay=miniuart-bt
    (This is not necessary, but we did it as the mini UART is less capable than the PL011 UART. If you skip this step you will have to use the mini Uart to communicate with the Roadrunner/Crazyflie.)
  • Disable login shell in raspi-config: Serial->disable Login Shell
  • Allow the user to use the Uart device: usermod -a -G dialout <user>

For more information about Pi UARTs have a look here:
https://www.raspberrypi.org/documentation/configuration/uart.md

Fell free to ask further questions if it does not work.
If it does I would be happy about a comment which steps you took to make it work to provide documentation that works for as many people as possible soon.

@BeyersBots

BeyersBots commented Mar 24, 2020 •

Copy link
Copy Markdown

@s9meboeh: Thank you for the quick response! I double checked to ensure I properly enabled uart. I re-accomplished each of the above. $ ls -l /dev lists serial0 -> ttyAMA0 which tells me I properly restored ttyAMA0 as primary uart. It also lists the groups as dialout. Typing $ groups lists the current user as part of the dialout group. Typing $ python3 -m serial.tools.list_ports -v provides

/dev/ttyAMA0
    desc: ttyAMA0
    hwid: 3f201000.serial
1 ports found

I have the Roadrunner connected to pins 4 (5V), 6 (Gnd), 8 (GPIO14), and 10 (GPIO15). I am worried that I did not update the firmware properly on the Roadrunner or am missing some other step. Unfortunately, I am working from home and cannot rebuild the firmware with my Windows machine as it keeps getting stuck on arm_iir_lattice_init_q31.o claiming the .c file is missing. Is CFLAGS += -DUART2_LINK_COMM enabled the only thing in firmware that needs to be changed?

I am downloading raspberry pi buster and will start with a clean build.

@BeyersBots

Copy link
Copy Markdown

Alright, restarted with a clean buster image. Pulled the latest crazyflie-lib-python version and went through the steps to set up the UART for serial communication. Same as before:

Crazyflies found:
serial://ttyAMA0
Connecting to serial://ttyAMA0

And then it just holds up. I'm guessing it is something with my Roadrunner firmware. I will continue to see if I can rebuild the firmware. Additionally, I'll try to verify that my UART is actually working. Thanks again for the help!

@BeyersBots

Copy link
Copy Markdown

I have confirmed serial is working with a basic gyroscope (BNO055) and running their simpletest.py using '/dev/ttyAMA0'. That would lead me to assume that it is the Roadrunner firmware or something I missed with the serialdriver.py. Any help would be greatly appreciated at this point!

@s9meboeh

Copy link
Copy Markdown
Contributor Author

@gimlisonofgloin
I was wondering if the aideck driver in the firmware that is compiled when you just add CFLAGS += -DUART2_LINK_COMM causes any problems because it also uses uart2 but differently but in my test it does not.
But I normally build the firmware with make UART2_LINK=1, which adds the UART2_LINK_COMM flag and excludes the ai-deck-driver to prevent complications there.

@BeyersBots

Copy link
Copy Markdown

I am somewhat new when it comes to compiling large projects like this. Would the command then be make PLATFORM=tag UART2_LINK=1? I will see if there are any of my students in the lab to try and build the firmware for me.

@s9meboeh

Copy link
Copy Markdown
Contributor Author

Yes that is the command I thought about. (Although I do not have a Roadrunner to test it.)
Which pins did you use on the Roadrunner? I think it should be this:
Roadrunner right 1 (TX2) -- Pi 10
Roadrunner right 2 (RX2) -- Pi 8

@BeyersBots

Copy link
Copy Markdown

Yes, those are the pins I am using. As soon as I hear back from my students I will let you know what I find.

@BeyersBots

BeyersBots commented Mar 25, 2020 •

Copy link
Copy Markdown

@s9meboeh: I got it working! What I assume happened is my student didn't properly make the firmware before sending it to me. I downloaded a VM and compiled it and it worked great. For those who want to repeat serial communication with RaspberryPi 3B+ Stretch or Buster.
First, accomplish UART settings per @s9meboeh:

  • Enable the mini UART (/dev/ttyS0) in /boot/config.txt: enable_uart=1
  • Restore ttyAMA0 as primary UART in /boot/config.txt: dtoverlay=miniuart-bt You can confirm that ttyAMA0 is the primary UART by checking for serial0 -> ttyAMA0 using $ ls -l /dev
  • Disable login shell in raspi-config: $ sudo raspi-config ->5. Interfacing Options -> P6. Serial->Select No for login shell and then Yes to enable serial port hardware. You can confirm this worked by checking that "console [ttyAMA0] enabled" does not show up when you run the command dmesg | grep tty
  • Allow the user to use the UART device: $ sudo usermod -a -G dialout $USER
  • Ensure the user group for the UART device is dialout using $ ls -l /dev/ttyAMA0 and the current user is part of the group dialout using $ groups

Second, update firmware for Roadrunner/Crazyflie per @knmcguire:

  • Edit the config.mk.example file to include CFLAGS += -DUART2_LINK_COMM at the end and rename to config.mk
  • Make the firmware and flash the Roadrunner/Crazyflie

Last, run the code:

  • Add pyserial dependency $ pip3 install pyserial
  • Run basiclog.py: $ python3 basiclog.py

Thanks again for all of the help and discussion. It is working great and does not hang up as it does using USB.

@knmcguire

Copy link
Copy Markdown
Contributor

@s9meboeh thank you for helping out! @gimlisonofgloin glad it is working for you now :) If you have any more problems, open up another issue here or use the forum.bitcraze.io.

@BeyersBots

BeyersBots commented Mar 25, 2020 •

Copy link
Copy Markdown

@s9meboeh , Can you think of any reason it would work with Python3 and not Python2.7? I followed all of the crazyflie-lib-python instructions using python2.7 and run the basiclog.py script and it just gets stuck at "Connecting to serial://ttyAMA0". I can connect to UART using python3 and I can connect to USB using python2.7. I just cannot connect to UART using python2.7. I've included all of the dependencies (pyserial, etc). The ROS version we are using with all of our other projects is using python2.7. Thanks again and sorry for all of the questions!

@knmcguire

knmcguire commented Mar 26, 2020 •

Copy link
Copy Markdown
Contributor

Hi, This will make sense since we stopped actively supporting python 2.7 after the latest release of the cflib, since python will be dropping support for 2.7 soon as well.

@BeyersBots

Copy link
Copy Markdown

Makes sense. I updated my ROS package to use Python3 so we are good to go! Thanks again!

@BeyersBots

BeyersBots commented Mar 27, 2020 •

Copy link
Copy Markdown

New commit requires adding enable_serial_driver=True to cflib.crtp.init_drivers() in basiclog.py to activate serial driver. Just adding that note in here to help consolidate all instructions for getting serial to work on Raspberry Pi. And because it gave me a headache most of the morning because I didn't realize there was a change when I pulled the repo today...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants