Beaglebone black Matlab Script

1 Ansicht (letzte 30 Tage)
Kapil
Kapil am 16 Feb. 2014
Beantwortet: Jorge am 3 Jun. 2015
Is there a way to connect and control the beaglebone black with matlab via Serial commands?
I am not looking for Simulink control, but rather direct code control? Does anyone have a script?
Thank you

Antworten (2)

Hong Ye
Hong Ye am 29 Okt. 2014

Jorge
Jorge am 3 Jun. 2015
Here are the steps for controlling the BeagleBone Black card from Matlab using a very simple program.
Hardware/Software used:
Windows Platforms: XP and 7 Hardware: BeagleBone Black BBB Operating System: Debian GNU/Linux 7 BeagleBone image: BeagleBone Debian Image 2014-04-23 Matlab version: R2009b g++ compliler. The Linux distributions come with a built-in c-compiler (gcc) and c++ compiler (g++). Example program: myprog from http://inspire.logicsupply.com/2014/07/beaglebone-c-programming-example.html Instructions
First, let’s create a file 'myprog.cpp' in BBB’s root directory using the nano editor (or vim, or emacs - if you prefer) Then, we will connect to the BeagleBone from Matlab and log in as 'root'.
STEP 1- Source Code Example program from elinux.org:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;
int main(){
cout << "LED Flash Start" << endl;
FILE *LEDHandle = NULL;
const char *LEDBrightness="/sys/class/leds/beaglebone:green:usr0/brightness";
for(int i=0; i<3; i++){
if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
fwrite("1", sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
}
usleep(1000000);
if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){ fwrite("0", sizeof(char), 1, LEDHandle); fclose(LEDHandle); } usleep(1000000); } cout << "LED Flash End" << endl; }
Program Overview
In this simple example program, we flash the built-in LED USR0 three times. We start out by sending a message to 'cout' (console output) to let us know that the program has started. Finally, a simple message is sent to indicate that we are done is sent to the console.
Step 2 - Compiling: g++ myprog.cpp -o myprog
This command calls the C++ compiler with the source file myprog.cpp and outputs (-o) the file myprog.
STEP 3- Running the Program within the BeagleBone: ./myprog
The program will flash the built-in USR0 LED three times, and display a message at the start and the end:
root@beaglebone:~# ./myprog LED Flash Start LED Flash End
STEP 4- Running the Program Manually from Matlab
Running the program manually in Matlab using basic serial port commands. Create a serial port object for serial port COM5 (check Windows Device Manager to find out the serial port used by the BeagleBone): s=serial('COM5');
Configure the Baudrate to 115,200: set(s,'BaudRate',115200);
Connect the serial port object to the BeagleBone:
fopen(s);
Write data to the BeagleBone. Here we log in as root: fprintf(s,'root')
Write data to the BeagleBone. This time execute the program myprog: fprintf(s,'./myprog')
Disconnect and clean up — When you no longer need the serial port object, disconnect it from the device using the fclose function: fclose(s)
Remove the serial port object from memory using the delete function: delete(s)
Remove the serial port object from the MATLAB workspace using the clear command: clear s;
If you encounter any of the messages listed below in Matlab just simply run the following command: delete(instrfind({'Port'},{'COM5'}))
then disconnect the USB cable from the BeagleBone momentarily and then connect it again before continuing.
??? Error using ==> serial.fopen at 72 Port: COM5 is not available. No ports are available. Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in ==> LED_tester>LED_tester_OpeningFcn at 77 fopen(s);
Error in ==> gui_mainfcn at 221 feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> LED_tester at 42 gui_mainfcn(gui_State, varargin{:});
This simple example program should help you get started with Matlab controlling the BeagleBone Black where you can go from here.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by