User Tools

Site Tools


lego_nxt_teleoperation_introduction

LEGO NXT Teleoperation Introduction
Author: Hashim H., Email: [email protected]
Date: Last modified on [08/04/24]
Keywords: NXT, Teleoperation

Motivation and Audience

The following tutorial has the motivation of education on teleoperation using the Lego NXT module using readily available peripherals. This tutorial assumes:

*BricxCC (Bricx Command Center) application setup
*A basic understanding of C++ (or general object oriented programming)

Parts List and Sources

Part Name/Description Vendor Vendor Number or URL Price QTY
Lego NXT Kit Lego N/A N/A 1
Sony Playstation 2 Controller Interface for NXT or EV3 mindsensors.com http://www.mindsensors.com/ev3-and-nxt/29-sony-playstation-2-controller-interface-for-nxt-or-ev3 $42.95 1

Software

Setup

Step 1

Ensure your NXT module is sufficiently powered with either a battery pack or six AA batteries and connect to your device hosting the BricxCC IDE via USB. It is suggested to have the USB-A end plugged in prior to the USB-B to avoid connectivity issues.

Step 2

After seating two AAA batteries in the PS2 controller, connect the PS2 controller receiver module (with the receiver connected) to the NXT on the port labeled “1” (*connecting via a letter port may cause damage to the receiver and/or module*), ensuring the connection is valid by actuating buttons on the controller as this should cause a green LED to activate on the receiver.

Step 3

Download the required NXC library and sample/demo on the controller product page listed above or here. It is highly recommended that you compile and run the included sample to understand how to interface with the library.

Programming

In any file using the PS2 library, always import the library file and define/instantiate related variables such as the module address (for communication), sensor port, and controller stat/pressure variables.

The following is an example of moving a motor connected to port A based on the input of the PS2 controller’s left stick:

basic_movement.nxc
//imports the library
#include "PSP-Nx-v4-lib.nxc"
 
//Defines receiver module address for I2C communication
#define ADDR     0x02
 
//Defines input port as 1 (change as seen fit)
const byte SensorPort  =  IN_1;
 
task main(){
 
//Initializes variables associated with button/stick inputs
  psp currState;
  psp pressures;
 
//Initializes PS2 controller connection
  PSPV4_Init(SensorPort, ADDR);
 
  while(true) {
 
//continuously reads controller button and stick values
    PSPV4_ReadButtonStateWithPressure(SensorPort, ADDR, currState, pressures);
 
//Puts current state of left joystick ranged [-100:100] into integer variable 
    int leftStick = currState.l_j_y;
 
//Move motor forward based on leftStick value
   OnFwd(OUT_A, leftStick);
  }
}

Given verification of user input given controller output, robotic teleoperation can be performed by building out the module with two motors in a manner similar to the domabot’s base (full instructions here) and determining output based on controller input.

The following are examples of functions to use in your main loop for teleoperation (assuming motors connected to ports B and C):

drive_functions.nxc
//Pass in the current state of a given joystick axis to the respective field(s)
void tankDrive(int rightStickY, int leftStickY){
  OnFwd(OUT_B, leftStickY);
  OnFwd(OUT_C, rightStickY);
}
 
void arcadeDrive(int rightStickX, int leftStickY){
  OnFwd(OUT_B, leftStickY+rightStickX);
  OnFwd(OUT_C, leftStickY-rightStickX);
}

Considering cases where peripheral devices/appendages such as those below are utilized for teleoperation, it is helpful to create functions defining their movement.

The following are example functions that may be used and adjusted based on peripheral construction and purpose:

peripheral_functions.nxc
//Rotates a motor forward and backward using the square button
//Can be used in mechanisms such as a gripper/claw and projectile “kicker”
//Fill variable “pressures.square” into respective field
 
//Boolean variables "closed" and "valid" are instantiated outside of function 
//as doing so within while loop resets the respective states each iteration
 
bool closed;
bool valid; 
 
void doUndo(int square){
 
    if(square == 100 && !valid){
      valid = true;
       if(!closed){
        closed = true;
        RotateMotor(OUT_A, 75, -50);
       }else if(closed){
        closed = false;
        RotateMotor(OUT_A, 75, 50);
      }
    }else if(square == 0 && valid){
      valid = false;
    }
 
}
 
//Rotates a motor continuously CW or CCW if holding a particular shoulder button
//Can be used in mechanisms such as a projectile “kicker”
//Insert variables “pressures.r2” and “pressures.l2” into respective fields
 
void continuousRotation(int rightBumper, int leftBumper){
 
    if(rightBumper == 100){
      OnFwd(OUT_A, 80);
    }else if(leftBumper == 100){
      OnRev(OUT_A, 80);
    }else{
      Off(OUT_A);
    }
 
}

Final Words

The purpose of this tutorial has been to inform of the potential for teleoperation using the Lego NXT module, a user utilizing a remote controller to execute defined functions which while explained for mechanical use, may extent far beyond such.

Speculating future work derived from this tutorial includes that of autonomous NXT module operation with an emphasis on function execution in response to visual data.

lego_nxt_teleoperation_introduction.txt · Last modified: 2024/08/04 16:08 by hhameed