Saturday 15 February 2014

Android smartphone control of a Lego NXT

Way back during Black Friday of 2013, I picked up a Kyocera Event Android smartphone on walmart.com for $20. I didn't intend to buy it to use as a phone, but rather as a robot contoller/sensor.
It has all the usual smartphone sensors, and runs Android 4.2.2 I think. Anyway, first thing I did when I got it up and running was to download an app that let me control my Lego NXT with both the touchscreen and accelerometer. Here is what insued:
A reasonably simple and mundane thing, but man that was very exciting for me. I have downloaded a few other apps designed to connect thru an HC-05 bluetooth module to an Arduino, which I recently reiceved in the mail, but have yet to try out. I think the phone will prove to be a very versitile addition to my robotics collection.

A fairly quick PKP

Since I have been back to school, I have not had much time to work on PK, but here is a quick update:
This is a video I took of the servo actuating the trigger with an CO2 source connected to the paintball gun. The red robot on the left side was the quickest thing I could come up with to drive a servo via manual control. The reason my thumb is on the servo arm is that the servo mounting is not very strudy (really thin piece of plastic as in the last post) so the servo arm has a tendancy to slip off of the trigger. And the CO2 was low, so I had to re-cock the paintball gun after every shot. Anyway, here it is:
And as well, I found the code running on the RC controller and the motor controller, here it is, RC controller first:
#include <Servo.h>
Servo left;
Servo right;
void setup()
{
  Serial.begin(9600);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  left.attach(9);
  right.attach(10);
}

void loop()
{
  int p1 = pulseIn(11, HIGH);
  int p2 = pulseIn(12, HIGH);
  p1 = constrain(p1, 1300, 1700);
  p2 = constrain(p2, 1300, 1700);
  p1 = map(p1, 1300, 1700, 0, 180);
  p2 = map(p2, 1300, 1700, 0, 180);
 
  if (p1 < 99 && p1 > 81)
  p1 = 90;
  if (p2 < 99 && p2 > 81)
  p2 = 90;
 
  left.write(p1);
  right.write(p2);
 
  Serial.print(p1);
  Serial.print("  ");
  Serial.println(p2);
  delay(10);
}

And the motor controller:
 #define in1 11
#define pwm1 9
#define f1 5
#define b1 7
#define in2 12
#define pwm2 10
#define f2 6
#define b2 8
#define failsafe 13
void setup()
{
  pinMode(failsafe, OUTPUT);
  // Channel 1:
  pinMode(in1,INPUT);
  pinMode(pwm1,OUTPUT);
  pinMode(f1,OUTPUT);
  pinMode(b1,OUTPUT);
  // Channel 2:
  pinMode(in2,INPUT);
  pinMode(pwm2,OUTPUT);
  pinMode(f2,OUTPUT);
  pinMode(b2,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
 int p1=pulseIn(in1,HIGH);
 int p2=pulseIn(in2,HIGH);
//========== Failsafe ================

 if (p1 == 0 || p2 == 0)
 {
   digitalWrite(failsafe,HIGH);
   digitalWrite(b1,LOW);
   digitalWrite(f1,LOW);
   digitalWrite(b2,LOW);
   digitalWrite(f2,LOW);
   analogWrite(pwm1,255);
   analogWrite(pwm2,255);
   delay(1);
 }
 else
 {
   digitalWrite(failsafe,LOW);
//============= Channel 1 =============
 if(p1 > 1550)
 {
   int s1=map(p1, 1550, 2000, 255, 0);
   analogWrite(pwm1,s1);
   digitalWrite(b1,LOW);
   digitalWrite(f1,HIGH);
 }
 else if(p1 < 1450)
 {
  int s1=map(p1, 1450, 0, 255, 0);
  analogWrite(pwm1,s1);
  digitalWrite(f1,LOW);
  digitalWrite(b1,HIGH);
 }
 else
 {
  analogWrite(pwm1,255);
  digitalWrite(b1,LOW);
  digitalWrite(f1,LOW);
 }
//============ Channel 2 ==============
 if(p2 > 1550)
 {
   int s2=map(p2, 1550, 2000, 255, 0);
   analogWrite(pwm2,s2);
   digitalWrite(b2,LOW);
   digitalWrite(f2,HIGH);
 }
 else if(p2 < 1450)
 {
  int s2=map(p2, 1450, 0, 255, 0);
  analogWrite(pwm2,s2);
  digitalWrite(f2,LOW);
  digitalWrite(b2,HIGH);
 }
 else
 {
  analogWrite(pwm2,255);
  digitalWrite(b2,LOW);
  digitalWrite(f2,LOW);
 }
//====================================
 }
}

And as well, I purchased rather cheaply another CO2 tank for my paintball gun (it is designed for use in a home soda maker, but it uses the same connector and holds the same amount of CO2 as a regular paintball tank)
 And this, candy despenser, I think? anyway, a nice source of acrlyic that will probably end up incorperated into PK somewhere:
And lastly, I purchased some PIR detectors for use in the targeting system, and found out that they sense motion, and pretty much ignore life for 3 seconds after that, which means I am going to have fun turning them into a tracking system, and might not be able to use them at all, but we will see. In addition, I purchased a few Arduino pro mini clones, and one will probably end up replacing the Micro Magician as the motor contoller brain, and an Arduino Mega 2560 for the main brains of PK. And that is the progress so far.