Stereo webcams on mounting plate. |
Before I begin to implement any type of image processing or complex algorithms on the Pi, I must establish a link between the Pi and my microcontroller that will be interfacing with the stepper motors. Right now I am using a Arduino micro for the ease of implementation, however I eventually want to move over to using a PIC18F2550 microcontroller for size considerations. Starting with the Arduino and then porting the code over to the PIC might help some beginners reading this do the same.
My Pi 3 is running Raspbian Jessie Lite, kernel version 4.4. Note that the following instructions are for the Pi 3, the UART port is changed on the Pi 3 from previous versions due to the addition Bluetooth. If you are familiar with using UART on previous Pi's, the UART lives at /dev/ttyAMA0. This port is now the connection to the Bluetooth controller. The UART on the Pi 3 is now at /dev/ttyS0. If you do not see this show up in your file listing, follow the bellow configuration and it should show up after.
Configuring the UART:
1) Stop and disable serial service
sudo systemctl stop serial-getty@ttyS0.serivce
sudo systemctl disable serial-getty@ttyS0.service
2) Change command line boot options
sudo nano /boot/cmdline.txt
You will see something similar to console=serial0,115200 in the file, remove this and any other mentions of serial0 and save the file.
Pi 3 header pinout. |
3) Enable the UART pins
sudo nano /boot/config.txt
Append enable_uart=1 to the bottom of the file and save.
4) Reboot your Pi.
You should now be ready to use the UART on the Pi 3. To test that the configuration works, I connected the UART TX pin, GPIO14, to my oscilloscope to observe the output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc, char* argv[]) {
struct termios serial;
char* str = "Hello, world!";
char* port = "/dev/ttyS0";
printf("Opening %s\n", port);
int uartPort = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (uartPort == -1) {
perror("Failed to open port\n");
return -1;
}
// Set up UART port
serial.c_iflag = 0;
serial.c_oflag = 0;
serial.c_lflag = 0;
serial.c_cflag = 0;
serial.c_cc[VMIN] = 0;
serial.c_cc[VTIME] = 0;
serial.c_cflag = B115200 | CS8 | CREAD;
tcsetattr(uartPort, TCSANOW, &serial); // Apply configuration
// Attempt to send and receive
printf("Sending - %s\n", str);
int r = write(uartPort, &str, strlen(str));
if (r < 0) {
perror("Failed to write to UART");
return -1;
}
else {
printf("Sent successful\n", r);
}
// Close port
close(uartPort);
}
Compile the above code with sudo gcc main.c -o main, where main.c is the name of the file the code is in, and then run the compiled code with ./mainThe output should look something like
Opening /dev/ttyS0
Sending - Hello, world!
Sent successful
The output capture on my scope verifies that the Pi 3 is transmitting our string from the UART. One important note about the waveform, the Pi 3's UART operates at 3.3V, however I have added in a transistor that switched 5V for future interfacing with the Arduino. This is why the waveform is on the 5V scale.
Scope waveform capture of "Hello, world!" |
No comments:
Post a Comment