Monday, September 12, 2016

T-shirt Cannon


CLICK ON THE PICTURE FOR DOCUMENTATION

Addendum... The T-shirt cannon now has an airhorn!


I figured this would be a good addition both for the impact and also for safety.  I purchased a couple kits from Amazon, this one and this one, the first as the original and the second when the plastic horns from the first kit broke.  The kits come with three parts... the horns, an electronic compressor, and a relay.  I thought the kit was broken because when I wired the compressor to 12V through a momentary switch and pressed the switch, no sound came out.  The compressor made a weak noise like it was a small outboard motor, but that was it.  The wires also got very hot.

I wired up the product in the manner instructed which uses a relay activated by a momentary switch to drive the compressor.  This diagram in Figure 1 shows how this wiring is done (taken from a video on youtube)
Figure 1
Figure 2
This worked well.  Next, I wanted to remove the human from the equation and automate the air horn.  I used a transistor in place of the momentary switch to turn on the relay.  A 2N2222A NPN transistor with a 220 Ohm resistor wired to the base is driven by the output of the Arduino Mega 1280 "brain" of the T-shirt cannon.  I fabricated a custom PCB board, the layout of which is shown below in Figure 2.  A diode, an LED/resistor, and the relay (pins 85 and 86 of the automotive relay) are in parallel between 12V and the transistor.  When the transistor is turned on, the relay is activated, and the LED turns on.  The flywheel diode takes up any reverse EMF generated when the magnetic field of the relay collapses, and thus protects the transistor.  Eagle CAD files for the PCB are available here.

Figure 3 shows the actual automotive relay and the pins labelled for reference/troubleshooting.
Figure 3
Figure 4 shows the layout of wiring on the actual T-shirt cannon (again for reference).
Figure 4



Wednesday, September 7, 2016

Pong Game: Joystick Controlled

This version of the Pong Game builds on the previous, but the paddles are controlled by a logitech controller's two joysticks.  This version of the game is for two players, each of which control one of the joysticks.  The game keeps track of the number of times that the ball has hit each player's paddle and displays this score on the screen.

Currently the program does not end nicely; there is a Null Pointer Exception which terminates the run when the ball runs off the edge of the screen.   I am not sure on the source of the exception.

The video below shows the game being played.

Much of the code is the same as the previous post including the classes Ball, Paddle, and ButtonListener.  The class Listener is similar, but now the mouseMoved method doesn't do anything.  GamePanel now includes two more labels which display the player's scores (scoreLabel1 and scoreLabel2).  It is also passed a JoyStickController object which gives axes to the method GetYAxesValues and the variables Y1Value and Y2Value which contain the most recent values of the two joysticks on the Logitech Controller (more on that below).


The picture shows that the left and right paddles' ycor variables are set to increase with the value of the left and right joysticks on the Logitech controller (respectively).  So if the left joystick is centered (Y1Value is zero), then leftPaddle.ycor stays constant.  If the left joystick is pressed up, then Y1Value is negative and leftPaddle.ycor decreases causing the leftPaddle to move up on the screen (recall that the y-coordinate increases as you move down the screen).  If the left joystick is pressed down, then Y1Value increases and the leftPaddle moves down the screen.  The multiplier *20 just makes the paddles move at a reasonable rate.

The class JoystickController includes two methods, SetController and GetYAxesValues.  The first method is called once when the main method of the PongGame class is run, and it scrolls through all the attached devices looking for one of type "Stick" .  It then sets a local variable (logitech) of type Controller to be that device.

The GetYAxesValues method calls a Controller method called poll, then sets an array called components to include the names of each of the parts of the Logitech controller.  It scrolls through the components array until it finds the array element called "Z Rotation" then stores the value of that component using getPollData in Y1Value.  Similarly, it stores the value of "Y Axis" in Y2Value.  Each value is subjected to a deadband, so that if the value is between -0.1 and 0.1 it is set to 0.

In the PongGame class, most everything is the same, but the main method now just creates an instance of PongGame, then called two methods (setupPlayers and initializeGame).  This is a much more elegant way compared to the mouse version, which does all the work in the main method.

Full game code can be found here:  https://github.com/gcronin/PongGame/blob/JoystickControl/PongGame/src/ponggame/PongGame.java .

Tuesday, September 6, 2016

NXT Security System

MIT App Inventor 2 is a great tool to develop Android apps.  It also has some robotic functionality as there is built in support for LEGO NXTs and EV3s.  I just finished a very small test program to try out this functionality.



I wrote an app which uses a light sensor and sound sensor on an NXT to simulate detecting an intruder. The light sensor normally has a laser pointer flooding the input, but if something crosses the path of the laser, the light reading drops much lower. This triggers an alarm which is indicated by a moving motor, a beeping sound, and a visual indicator ("Alarm!") on the Android phone. Similarly, a sound above the threshold (set at 125) on the sound sensor trips the alarm. The Android interface allows you to turn on and off the alarm, shows the values of the sound and light sensors, and shows whether you are connected to the NXT.

When creating an app in MIT App inventor, you first create the interface (Designer) then create the code (Blocks).  Coding is drag and drop just like Scratch or Enchanting.   The interface for this app is shown below:

There are several button to connect and disconnect to an NXT over Bluetooth, and to turn on or off the security system.  There are also several NXT specific objects: an NXT Sound Sensor, an NXT Light Sensor, and an NXT Direct Commands (which gives access to motor control).  A clock is setup with a one second increment.  Finally, there are several "labels" which are places where text is displayed.  One label (status) shows whether the alarm is on, off, or triggered.  Another label (SoundReading) shows the sound sensor reading or "not connected" if there is no Bluetooth connection.  The last label (misnamed UltrasonicReading from previous experiments) shows the light reading or or "not connected".

The coding is all event driven.  Events can be triggered by pressing a button on the App interface, by a recurrent timer, or by certain sensor criteria being met.
The Connect to Robot and Disconnect buttons give Bluetooth functionality.  When you click the Connect To Robot button (which is actually a list picker), it brings up a list of available Bluetooth devices.  When you select one, the App makes a connection to that NXT.  The Disconnect button ends that connection.

Two global variables are used to control whether the alarm is on (on?), and whether it has been triggered (AlarmTripped?).  The OnButton sets on? to true.  Once that criteria is met, either a NXTSoundSensor.AboveRange or a NXTLightSensor.BelowRange event can make the second variable (AlarmTripped?) true.

A clock event is triggered every second.  This event displays the sound and light sensor readings if there is a Bluetooth connection.  Otherwise it displays "Not Connected".  If the value of AlarmTripper? is true, it plays a tone, and spins a motor.  Because the duration of the tone is half of a second and the clock event is triggered every second, the overall aural effect is a periodic beeping sound which sounds like an alarm.

The OffButton resets both global variables to false and stops the motor.

Overall, this was a simple but great way to learn MIT App Inventor 2.  The project has been published on the App Inventor 2 website:  http://ai2.appinventor.mit.edu/?locale=en#4626597671075840.