Arduino Jaguar‎ > ‎

JagWii

Control a Jaguar with the Wii Nunchuck!




Uses the WiiChuckClass and borrows heavily from the example code.
Comments and pictures to follow(soon-ish)!

//code starts here 
#include <math.h> //I don't remember which math function, but a good thing to have
#include <avr/interrupt.h> // Allows the PWM frequency to be modified
#include "Wire.h" 
#include "WiiChuck.h" // Need to create a file called WiiChuck.h and save into your library
//#include "nunchuck_funcs.h"

#define MAXANGLE 90
#define MINANGLE -90

// Setup the PWM related pins
const int jagsigpin = 9;
const int jagpwrpin = 7;
const int jaggndpin = 8;

//Setup an LED status pin
const int ledpin = 13;

//value is what the PWM is set to: 47 corresponds to midpoint, 20 full reverse 74 full forward
int value = 47;

//create a WiiChuck object called chuck
WiiChuck chuck = WiiChuck();
int angleStart, currentAngle;
int tillerStart = 0;
double angle;
int bob = 0;
boolean lightstatus = LOW;

void setup() {
    //set the PWM frequency to be 122Hz
  TCCR1B = TCCR1B & 0b11111000 | 0x04;

//setup the output pins for PWM and references
  pinMode(jagsigpin, OUTPUT);
  pinMode(jagpwrpin, OUTPUT);
  pinMode(jaggndpin, OUTPUT);

//setup the LED indicator pin
  pinMode(ledpin, OUTPUT);

//set the reference pins
  digitalWrite(jagpwrpin,HIGH);
  digitalWrite(jaggndpin,LOW);

//start the serial link for monitoring purposes
  Serial.begin(115200);
//call the nunchuck initialization functions
  chuck.begin();
  chuck.update();
//set the PWM value to midrange so the motor doesn't jump around before ready
  analogWrite(jagsigpin,value);
  //chuck.calibrateJoy();
}


void loop() {
//20mS loop provides a smooth response that seems continuous
  delay(20);
//get the values from the nunchuck over the I2C link
  chuck.update(); 

//bob is a counter variable for the loop that allows things to run on multiples of 20mS
  bob = bob + 1;
//50x20mS = 1000mS = 1second, 
  if(bob == 50){
  //toggle the boolean value
  lightstatus = ~lightstatus;
//write the new value to the LED pin to change the light
  digitalWrite(13, lightstatus);
//clear the counter so the light will change again in another second
  bob = 0;
  }
  
//the original code scrolled the axis values on the terminal. It was difficult
//to understand so I have the display update once, each time that c is pressed.
  if(chuck.cPressed()){
  
  Serial.print(chuck.readRoll());
    Serial.print(", ");  
  Serial.print(chuck.readPitch());
    Serial.print(", ");  

    Serial.print((int)chuck.readAccelX()); 
    Serial.print(", ");  
    Serial.print((int)chuck.readAccelY()); 
    Serial.print(", ");  

    Serial.print((int)chuck.readAccelZ()); 

    Serial.println();
  }
// the original code didn't grab the thumbstick data which makes for better motor control
// while debugging I updated the motor speed only on button press
  if(chuck.zPressed()){
    Serial.print("x axis: ");
    Serial.print((int)chuck.readJoyX());
    Serial.print(", y axis: ");
    Serial.print((int)chuck.readJoyY());
    Serial.println();
/*The PWM value used allows a range of 20 to 74 with 47 as the midpoint
there are 27 steps in the positive and negative directions
The nunchuck function provides -100 to 100 
this scale is mapped to -27 to 27 and then subtracted from the midpoint of 47
*/
    value = ((int)chuck.readJoyX())*(.27);
    value = value/2;
    value = 47-value;
    Serial.print("value written: ");
    Serial.print(value);
    Serial.println();
    analogWrite(jagsigpin,value);
  }
// For continuous running with the loop... This is where the real action occurs
  value = ((int)chuck.readJoyX())*(.27);
    value = value/2;
    value = 47-value;
    Serial.print("value written: ");
    Serial.print(value);
    Serial.println();
    analogWrite(jagsigpin,value);
}

Comments