marvin:ecp5
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
marvin:ecp5 [2009/01/23 10:23] – sohn | marvin:ecp5 [2009/01/29 10:09] – deva | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | =====Stikord===== | + | <texit info> |
- | * Stadigt voksende klassediagram | + | author=Johnny Rieper, Bent Bisballe Nyeng and Kasper Sohn |
- | * Bluetooth | + | title=Marvin - The Balancing Robot. |
+ | </ | ||
- | ======End course project, lab report 5====== | + | ======Lab report 5 - Bluetooth controlled robot====== |
**Date:** January 19nd 2009\\ | **Date:** January 19nd 2009\\ | ||
**Duration of activity:** 8-16\\ | **Duration of activity:** 8-16\\ | ||
Line 9: | Line 10: | ||
=====Project Goal===== | =====Project Goal===== | ||
- | Make robot communicate | + | Make robot remote controlled from the PC through bluetooth. |
=====Plan===== | =====Plan===== | ||
* Make a bluetooth client code Behaviour on the Marvin, that can receive numbers and act on them. | * Make a bluetooth client code Behaviour on the Marvin, that can receive numbers and act on them. | ||
- | * Make a simple | + | * Make PC bluetooth application read from keyboard arrows to determine control direction. |
+ | |||
+ | =====The protocol===== | ||
+ | {{ : | ||
+ | |||
+ | From wikipedia: //" | ||
+ | LeJOS has built an entire framework for bluetooth communication, | ||
+ | (([[http:// | ||
+ | We do not want to go further into the details of the protocol itself, but the way the device is shared can eventually end up in data loss(([[http:// | ||
+ | |||
+ | On the PC LeJOS has also made a bluetooth interface. This works exactly the same as the one on the NXT, and we therefore simply had to make a connection (listening on the NXT and connecting on the PC) and then creating input/ | ||
+ | |||
+ | LeJOS has put a lot of effort into making the bluetooth communication easy to use, and the implementation were therefore quite straight forward. Observe the resulting code below. | ||
+ | |||
+ | We experienced a lot of slowdown in the balancing thread whenever the bluetooth communication was running, and we had to disable it entirely at numerous occasions to avoid the robot falling over.\\ | ||
+ | We later discovered | ||
+ | |||
+ | Here is a movie of the resulting remote controlled robot, with all other behaviours disabled: | ||
+ | [[http:// | ||
+ | {{youtube> | ||
+ | |||
+ | As it can be seen from the video, Marvin is not exactly reacting real-time | ||
+ | |||
+ | =====The BTControl class===== | ||
+ | {{ : | ||
+ | Bluetooth control behaviour. It reads integers from a bluetooth input stream, interpreting them as keycodes. | ||
+ | It reacts only on the keycodes connected with the arrow keys (up, down, left, right) and tells Marvin to drive accordingly. | ||
+ | |||
+ | ^ Key ^ KeyCode ^ Variable | ||
+ | | left | 37 | directionLeft | ||
+ | | right | 39 | directionRight | ||
+ | | up | 38 | directionForward | ||
+ | | down | 40 | directionBackward | | ||
+ | |||
+ | All exception handlers have been removed to improve on readability: | ||
+ | <code java> | ||
+ | public class BTController extends Behavior | ||
+ | { | ||
+ | . | ||
+ | . | ||
+ | . | ||
+ | |||
+ | public void run() | ||
+ | { | ||
+ | NXTConnection conn = Bluetooth.waitForConnection(); | ||
+ | DataInputStream istream = conn.openDataInputStream(); | ||
+ | |||
+ | while(true) { | ||
+ | if(istream.available() > 0) direction = istream.readInt(); | ||
+ | else delay(200); | ||
+ | |||
+ | if(direction >= directionLeft && direction <= directionBackward) { | ||
+ | suppressLower(); | ||
+ | switch(direction) { | ||
+ | case directionLeft: | ||
+ | left(200); | ||
+ | |||
+ | |||
+ | break; | ||
+ | |||
+ | case directionRight: | ||
+ | right(200); | ||
+ | break; | ||
+ | |||
+ | case directionForward: | ||
+ | forward(200); | ||
+ | break; | ||
+ | |||
+ | case directionBackward: | ||
+ | backward(200); | ||
+ | break; | ||
+ | } | ||
+ | unsuppressLower(); | ||
+ | |||
+ | // Empty input buffer... we only want to use the latest keystroke. | ||
+ | while(istream.available() > 1) istream.readInt(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | On the PC, a window (JFrame) captures keyboard input and redirects their keycode values to the bluetooth stream: | ||
+ | <code java> | ||
+ | NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH); | ||
+ | NXTInfo nxtInfo = new NXTInfo(" | ||
+ | nxtComm.open(nxtInfo); | ||
+ | |||
+ | OutputStream os = nxtComm.getOutputStream(); | ||
+ | final DataOutputStream dos = new DataOutputStream(os); | ||
+ | |||
+ | JFrame frame = ... | ||
+ | |||
+ | frame.addKeyListener(new KeyListener() { | ||
+ | public void keyPressed(KeyEvent e) { | ||
+ | dos.writeInt(e.getKeyCode()); | ||
+ | dos.flush(); | ||
+ | } | ||
+ | public void keyReleased(KeyEvent e) {} | ||
+ | public void keyTyped(KeyEvent e) {} | ||
+ | }); | ||
+ | </ | ||
+ | The full source code for both of these classes can be found in the files '' |
marvin/ecp5.txt · Last modified: 2009/01/29 10:58 by rieper