How to Use the BeagleBone Black to Control the NXT Brick Through Bluetooth

Similar documents
Project Spartan s Bluetooth Guide

EVShield Interface Specifications

Protocol of data exchange with modem via USB interface Version

NXShield Interface Specifications

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Hands-on Lab. Lego Communications I2C Basics

Survey. Motivation 29.5 / 40 class is required

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Graphical LCD Display Datasheet EB

Sample F4T 32-bit Modbus RTU Packet

Bluetooth Keyboard Setup Instructions

EV3 Programming Workshop for FLL Coaches

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

Introduction to Lab 2

Rapoo E6700 Setup Instructions

Bluetooth programming for Linux

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

The RS-485 user manual for B800 series communication

LEGO RCX Hitachi H8/3292

Tap BLE API Documentation

robotics/ openel.h File Reference Macros Macro Definition Documentation Typedefs Functions

EL2310 Scientific Programming

MODBUS Protocol. The ecode Encoders contain both single register (16 bit) and double register (32 bit) values.

Sockets 15H2. Inshik Song

We would like to find the value of the right-hand side of this equation. We know that

Overview and Characteristics of Servo A1-16. General Servo Motor Specifications. Dimensions of Servo Motor. Wiring Connection

ECE331 Homework 4. Due Monday, August 13, 2018 (via Moodle)

Socket Programming for TCP and UDP

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Sample F4T 32-bit Modbus RTU Packet

16-311: Getting Started with ROBOTC and the. LEGO Mindstorms NXT. Aurora Qian, Billy Zhu

Tag Interface. 1. Introduction. 2. Control Modes. Olufemi Omojola, Rich Fletcher Physics and Media Group MIT Media Lab, January 2000

Introduction to Lab 2

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Networking 2. IP over Bluetooth

The MMDVM Specification ( )

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

TORRIX RS485. Technical Documentation. with MODBUS Protocol. Edition: Version: 3 Art. no.:

API Guide MSS-8 and MVS-16

1 Introduction. 1.1 Overview. 1.2 Supported Hardware. 1.3 Recommended Hardware. 1.4 Installation

Introduction to Mechatronics

Ch Mindstorms Package User s Guide

Index. Cambridge University Press Bluetooth Essentials for Programmers Albert S. Huang and Larry Rudolph. Index.

Colin Harman and Jonathan Kim

Texas Instruments Microcontroller HOW-TO GUIDE Interfacing Keypad with MSP430F5529

Tutorial: Making Legobot Move Steering Command Brighton H.S Engineering By: Matthew Jourden

Hardware: Logical View

ESP-NOW User Guide Version 1.0 Copyright 2016

Iwconfig Wlan0 Key Error For Wireless Request Set Encode

Hands-on Lab. Lego Programming BricxCC Basics

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Programming in C++ 4. The lexical basis of C++

R&D Centre: GT Silicon Pvt Ltd 171, MIG, Awadhpuri, Block B, Lakhanpur, Kanpur (UP), India, PIN

CODA Online Data Formats

Beginning C Programming for Engineers

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Specification E2 Interface

PRE32 RS232 protocol v1.21 Tomas Andersson

TECH TIP. Tritex Modbus Protocol Specification

Embedded Systems - FS 2018

Fun with Java Technology on Lego Mindstorms

VZ8(6)9 rev B I2C communication quick manual. SGX Sensortech

Hands-on Lab: LabVIEW Angle Sensor

Embedded Systems - FS 2018

Do not start the test until instructed to do so!

Part A: Monitoring the Rotational Sensors of the Motor

Hardware interface and protocol of data exchange with mobile beacon via USB, UART and SPI interfaces.

Robotics Study Material School Level 1 Semester 2

Hide & Seek Project. Table of Contents. 1 P a g e

Sample F4T 32-bit Modbus TCP Packet

Controller Area Network Global Positioning System Receiver and Real Time Clock. Copyright EVTV LLC EVTV CAN-GPS 3.01

Robotics II. Module 1: Introduction to Data & Advanced Programming Blocks

What is NXTCam5. What you will need before using NXTCam5. NXTCam5 User Guide

Project 15 - Reading a keypad with the Raspberry Pi

Grove - OLED Display 0.96''

User Manual SM137 & SM140 motors. Serial Communications Protocol

IF96017 MODBUS COMMUNICATION PROTOCOL

Homework 9: Software Design Considerations

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00

COPYRIGHTED MATERIAL. Introducing LEGO CHAPTER. Did you get a box that looks like the one shown in Figure 1-1?

Aeroflex Colorado Springs Application Note

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

SERVO-DRIVE PROGRAMMABLE STEP MOTOR CONTROLLER R LAN

2G Actuator Communications Protocol Document Rotary & Linear Actuators

PRODUCT RELIABILITY HOST COMMUNICATIONS PACKET STRUCTURE SPECIFICATION. % of Initial Brightness (New Module) NOTE. <type><data_length><data><crc> type

EL6483: Brief Overview of C Programming Language

Java using LEGO Mindstorms and LeJOS. University of Idaho

Custom Installation Notes: Serial programming interface and IR remote commands for Arcam UDP411 BD Player

CLIENT-SIDE PROGRAMMING

Modern Robotics Inc. Sensor Documentation

Project from Real-Time Systems Lego Mindstorms EV3

CTT MODBUS-RTU COMMUNICATION PROTOCOL TEMPERATURE MONITOR DEVICE

LEGO MINDSTORMS NXT Lab 4

Circuit Playground Firmata

bytes per disk block (a block is usually called sector in the disk drive literature), sectors in each track, read/write heads, and cylinders (tracks).

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

ESP8266 Application Note Firmware Download Protocol

STSPIN logic core basics. L6470\72, L6480\82 and powerstep01

CSE351: Memory, Data, & Addressing I

Transcription:

CMPT 433 How To Guide Team FID Ian Fong Fred Shih David Baik How to Use the BeagleBone Black to Control the NXT Brick Through Bluetooth Why Write your Own Library Unfortunately, there simply isn t a high quality bluetooth NXT library available in C. The more libraries are implemented in python (Python NXT) and.net (MindSqualls). The closest third party library available is for C++. This is due to several reasons, the first is that it s tough to write a cross platform library because of the way the packets are designed, the second is that it is a tedious and frustrating experience to do so. However, even though the networking is difficult, the NXT platform does make hardware design and implementation much simpler, and even though it is difficult, it is not impossible to get communication working reliably. This guide focuses on showing you the general steps to implement commands from LEGO s bluetooth development manual. Connecting the NXT Brick to BBB Pairing the Devices via Command Line 1. Turn NXT Brick s Bluetooth on a. Navigate to bluetooth > set on/off > on 2. Pair up the 2 devices through bluetooth a. On BBB, install the Bluetooth libraries: # sudo apt get install bluetooth # sudo apt get install bluez b. Search up the NXT s MAC address: # hcitool scan Scanning... 00:16:53:07:36:B4 NXT note: your MAC address will be different for your NXT Brick c. Pair up the two devices: # bluez simple agent hci0 <NXT s MAC address>

The console will prompt for a PIN Default pin on the NXT is 1234 To reset default pin on NXT: Bluetooth > Use Default Pswd > yes d. Register the NXT as a trusted device: # bluez test device trusted <NXT s MAC address> yes To test if the NXT is trusted, run: #bluez test device trusted <NXT s MAC address> Returns 1 if the device is trusted e. Connect the devices: # bluez test input connect <NXT s MAC address> The NXT should now be paired up with the BBB Connecting to the Brick Through C 1. Required libraries: #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> #include <sys/socket.h> 2. Pass in your MAC address as a string, then convert the address to a Bluetooth address using str2ba() 3. Create an address of struct sockaddr_rc 4. Initialize a socket using the socket() function socket(af_bluetooth, SOCK_STREAM, BTPROTO_RFCOMM); 5. Set the socket type to bluetooth, and select a channel: <address struct>.rc_family = AF_BLUETOOTH; <address struct>.rc_channel = (uint8_t) 1; 6. Connect using the address and socket connect(socket, (<struct sockaddr *)& <address struct>, sizeof(<address struct>));

A successful Bluetooth connection to the NXT will show a < next to the Bluetooth symbol Demo of Initializing and Connecting via Socket: void init_bluetooth(const char *bt_addr, int *socket) { struct sockaddr_rc addr = {0}; int status; *socket = socket(af_bluetooth, SOCK_STREAM, BTPROTO_RFCOMM); } addr.rc_family = AF_BLUETOOTH; addr.rc_channel = (uint8_t) 1; str2ba(bt_addr, &addr.rc_bdaddr); status = connect(*nxt_socket, (struct sockaddr *)&addr, sizeof(addr)); if(status < 0) { //error connecting to Bluetooth }

Communicating with the NXT The NXT Brick Command and Response packet formats can be obtained by reading Appendix 2 of LEGO s NXT Bluetooth Developer manual. The fields values are usually given as hexadecimal, not decimal! The basic layout of packets are as follow, each block represents one byte: a. Command packets: Command type: 0x00 to have the NXT return a response 0x80 to have the NXT not return a response The LSB and MSB compose the length of the packet, excluding the LSB and fields. Because no command is ever larger than 255 bytes, the MSB field is always zero. Command specific bytes always come after the command field. b. Response packets: Both Command and Response s first two bytes

(LSB and MSB) indicate the length of the package Like the command packets, the response packets also have the LSB and MSB fields which specifies the byte size of the response packet (excluding the LSB and MSB fields themselves). There is also a status byte which follows the MSB byte. A value of 0x00 denotes success, and anything else is a mapped error code. Note: you will only get a response if the command type is set to 0x00. c. Note: NXT commands are little endian 2. Commands are sent to the NXT through the socket via the write() function. Note that the length from the lsb/msb fields is different from the size of the packet. The size parameter for the write command is the size of the struct or array you chose to represent the command. write(socket, command, size of packet) 3. Responses can be read through the socket using the read() function. Note that this is a blocking wait, which means that if you forget to ask for a response in your initial command packet, the program will end up hanging at read. read(socket, response, size of response) Demo of Sending a Command to Motor A via Socket: //struct to a Motor Command //note the packed attribute typedef attribute (( packed )) motor_cmd_t { uint8_t lsb; uint8_t msb; uint8_t cmd_type; uint8_t cmd; uint8_t motor; uint8_t speed; uint8_t mode; uint8_t regulation; uint8_t ratio; uint8_t state; union { uint8_t tacho_limit[4]; uint32_t tacho; }; } SetMotor;

void run_motor_a(const int *nxt_socket) { //initialize the Motor Command SetMotor cmd = { 0x0C, //LSB 0x00, //MSB 0x80, //Command Type 0x04, //Command 0x00, //Motor A 0x64, //Speed 100 0x01, //Mode 0x00, //Regulation off 0x00, //Turn Ratio 0x20, //Run State {{ 0,0,0,0 }} //Tacho Limit }; } //send command to the NXT Brick if (write(socket, cmd, sizeof(cmd)) < 0) { perror("error sending command"); abort(); } int main() { int socket = 0; init_bluetooth( 00:16:53:07:36:B4, &socket); run_motor_a(socket); } //Close Bluetooth connection close(socket); Note: If a struct is used over a byte array, you must tell the compiler to pack the fields. On GCC and Clang, this is accomplished by adding the packed attribute (as shown above). If you do not do this, the compiler will most likely add padding, which will give you result in garbage transmissions between BBB and NXT.

Quick Reference Guide to NXT Packets Because LEGO frequently changes the URL for the bluetooth development sdk, we have embedded a table of useful packets in this guide. a. Motor Commands Byte Type Description 0 LSB Least significant byte 0x0C set command 0x03 read command 1 MSB 0x00 Most significant byte 2 Command Type 0x00 with response (useful when reading motor values) 0x80 no response 3 Command 0x04 to set command to motor 0x06 to read values from motor 4 Motor Specify motor of use 0x00 Motor A 0x01 Motor B 0x02 Motor C 0xFF All motors 5 Speed/Power Desired speed to set to motor Between -0x64 (-100) to 0x64 (100) 6 Mode 0x01 to turn on motor 0x02 to turn use brakes when stopping motor 0x04 to turn on regulation

7 Regulation 0x00 no regulation 0x01 to run motor at the specified Speed 0x02 to set 2 or more motors to a synchronized speed 8 Turn Ratio Between -0x64 (-100) to 0x64 (100) 9 Run State 0x00 turn off motor 0x20 turn on motor 10-14 Tacho Limit the amount of degrees to rotate the motors b. Motor Response Byte Type Description 0 LSB 0x19 Least significant byte 1 MSB 0x00 Most significant byte 2 Response Byte 0x02 3 Response Byte 0x06 4 Status Status of the motor 5 Motor Motor of the returned values 6 Speed/Power Current speed of motor 7 Mode Current mode 8 Regulation Current regulation 9 Turn Ratio Current turn ratio 10 Run State Current Runstate 11-14 Tacho Limit Current Tacho Limit 15-18 Tacho Count Distance the motor has rotated from last reset position 19-22 Block Tacho Count Position of the motor from most recent command 23-26 Rotation Count Position of the motor from last reset position

c. Motor Reset Command Byte Type Description 0 LSB 0x04 Least significant byte 1 MSB 0x00 Most significant byte 2 Command Type 0x00 with response 0x80 no response 3 Command 0x0A 4 Motor Specify motor of use 0x00 Motor A 0x01 Motor B 0x02 Motor C 5 Relativity TRUE to set position relative to last movement FALSE to set to absolute position

8. Sensor Commands and Responses a. Sensor Command Byte Type Description 0 LSB Least significant byte 0x05 to send command to sensor 0x03to read sensor 1 MSB 0x00 Most significant byte 2 Command Type 0x05 to set command to sensor 0x07 to read values from sensor 3 Command 0x00 with response (useful when reading motor values) 0x80 no response 4 Port Specify sensor port 0x00 Port 1 0x01 Port 2 0x02 Port 3 0x03 Port 4 5 Sensor Type values are from LEGO MINDSTORMS NXT documentations in the references 0x0B Low speed 9V 6 Sensor Mode values are from LEGO MINDSTORMS NXT documentations in the references 0x00 Raw Mode

b. Sensor Response Byte Type Description 0 LSB 0x0F Least significant byte 1 MSB 0x00 Most significant byte 2 Response Byte 0x02 3 Response Byte 0x07 4 Status Status of sensor 5 Port Port of the sensor 6 Validity TRUE if data is valid 7 Calibration TRUE if data has been calibrated 8 Sensor Type Current Sensor type 9 Sensor Mode Sensor Mode 10-11 Raw value Raw value 12-13 Normalized Value Normalized value 14-15 Scaled Value Scaled value 16-17 Calibrated Value Calibrated value

Cited Guides and Documentation 1. Lego mindstorm bluetooth reference manual v1.00 2. Gentoo Wiki - Device Pairing: http://wiki.gentoo.org/wiki/bluetooth#device_pairing 3. NXT LCD Image: http://nnxt.blogspot.ca/2012/10/motorcontrol-nxt.html