Pages: [1]   Go Down
Author Topic: would somebody like to help with a program to make a submarine?  (Read 2135 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very very new to this and I can make little things happen with the arduino.  But with what I would like to do I need some expert help. 
1. I have the arduino reading bytes from a wii nunchuck
2. what I need is to control two ESC's for motors.  As I move then nunchuck forward I need both motors on at the same speed.
3. when I need to turn I will have to have one motor spin up and the other stay off.
4. I need to outputs for a 5v relay to control my ballast pumps. 
  That is all i need
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds like fun.  If you are near me (Eastern Massachusetts, USA) then I'd love to collaborate.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think that remote control sub's use ballast tanks. Model sub (or so I'm told) dive on the dive planes alone, so that if the power fails or the sub runs into something it will just surface.

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well the submarine is going to have a cable connected to it.  Because wireless is way to unreliable in water.  So if something were to happen then i can just pull it back up myself.  I live in KS but some programming help is all I need I have everything ready just need to get a program going.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have everything ready just need to get a program going.

First thing needed to develop software on distant hardware:

        What devices are connected to what pins?

Second thing:

      How, exactly, do you want the nunchuck inputs to relate to the two motor speeds?
      The accelerometer can detect pitch and roll (but not yaw) relative to gravity or sudden accelerations in any direction.
      You also have a joystick and some buttons.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok,  This is what I have so far.  This comunicates with the arduino and all I am using is the joystick y,x axix and the two buttons. 

The buttons are just driving a 5v relay.  The problem with What I have right now is that both can be turned on and that would not be good for my battery.  I need to make it so that either one or the other is on and not both.  These are used on pins 2 and 4.  Also these are all set to ledpins because that is what I was doing testing with. 

Now the other part i need is to make the  pin 8 and pin 9.  work together.  When both of these see the input of of joystick y go up then both of these need to have the same PWM coming out.  Which the duty cycle at nuetral needs to be a about 6% and and the make duty cycle out needs to be about 8%. ( I will need to be able to adjust these slighly.) This will drive the submarine forward.  Now to turn, I need x joystick.  when the bytes go down then the pin eight needs to start backing off on the PWM.  Then if x joystick goes up then the Pin 9 needs to start backing off.  This will in turn make it so that one motor is spinning faster than the other and it will turn. 

Throught this code.  I have blocked out some things that are not needed.  and aslo I have added the thresh holds.  If the code was setup so that I could put in the thresholds from the wii nunchuck myself then it would be great


#include <PString.h>

#include <Wire.h>


#undef int
#include <stdio.h>


uint8_t outbuf[6];      // array to store arduino output
int cnt = 0;
int ledPin = 13;
int ledpin1 = 9;
int ledpin3 = 2;
int ledpin2 = 8;
int ledpin4 = 4;
int yThres = 200;
int xThres = 200;
int xThresmin = 40;
int z_buttonmin = 0;
int c_buttonmin = 0;
void
setup ()

{
  pinMode (ledpin1, OUTPUT);
  pinMode (ledpin2, OUTPUT);
  pinMode (ledpin3, OUTPUT);
  pinMode (ledpin4, OUTPUT);
  Serial.begin (19200);
  Serial.print ("Finished setup\n");
  Wire.begin ();      // join i2c bus with address 0x52
  TWBR = 158;
  TWSR |= _BV (TWPS0);
  nunchuck_init (); // send the initilization handshake
 
}

void
nunchuck_init ()
{
  Wire.beginTransmission (0x52);   // transmit to device 0x52
  Wire.write (0x40);      // sends memory address
  Wire.write (0x00);      // sends sent a zero. 
  Wire.endTransmission ();   // stop transmitting
}

void
send_zero ()
{
  Wire.beginTransmission (0x52);   // transmit to device 0x52
  Wire.write (0x00);      // sends one byte
  Wire.endTransmission ();   // stop transmitting
}

void
clearTwiInputBuffer(void)
{
  while ( Wire.available())
    Wire.read ();
}

void
loop ()
{
  int test =0;
  Wire.requestFrom (0x52, 6);   // request data from nunchuck
  while (Wire.available ())
    {
      outbuf[cnt] = nunchuk_decode_byte (Wire.read ());   // receive byte as an integer
      digitalWrite (ledPin, HIGH);   // sets the LED on
         
     
      cnt++;
    }

  clearTwiInputBuffer();
  // If we recieved the 6 bytes, then go print them
  if (cnt >= 5)
    {
      print ();
    }
 
  cnt = 0;
  send_zero (); // send the request for next bytes
  delay (100);
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void
print ()
{
  int joy_x_axis = outbuf[0];
  int joy_y_axis = outbuf[1];
//  int accel_x_axis = outbuf[2] * 2 * 2;
//  int accel_y_axis = outbuf[3] * 2 * 2;
//  int accel_z_axis = outbuf[4] * 2 * 2;

  int z_button = 0;
  int c_button = 0;

 // byte outbuf[5] contains bits for z and c buttons
 // it also contains the least significant bits for the accelerometer data
 // so we have to check each bit of byte outbuf[5]
  if ((outbuf[5] >> 0) & 1)
    {
      z_button = 1;
    }
  if ((outbuf[5] >> 1) & 1)
    {
      c_button = 1;
    }

//  if ((outbuf[5] >> 2) & 1)
//    {
//      accel_x_axis += 2;
//    }
//  if ((outbuf[5] >> 3) & 1)
//    {
//      accel_x_axis += 1;
//    }
//
//  if ((outbuf[5] >> 4) & 1)
//    {
//      accel_y_axis += 2;
//    }
//  if ((outbuf[5] >> 5) & 1)
//    {
//      accel_y_axis += 1;
//    }
//
//  if ((outbuf[5] >> 6) & 1)
//    {
//      accel_z_axis += 2;
//    }
//  if ((outbuf[5] >> 7) & 1)
//    {
//      accel_z_axis += 1;
//    }
   
  if (joy_y_axis > yThres)
  {
    if (joy_y_axis==0){
    return;
  }
  if (joy_y_axis==255){
    return;
  }
    digitalWrite (ledpin1, HIGH);
    digitalWrite (ledpin2, HIGH);
  }
  else
  {
    digitalWrite (ledpin1, LOW);
    digitalWrite (ledpin2, LOW);
  }
  if (joy_x_axis==0){
    return;
    }
  if (joy_x_axis==255){
    return;
  }
  if (joy_x_axis > xThres)
  {
    digitalWrite (ledpin2, HIGH);
  }
  if (joy_x_axis < xThresmin)
  {
    digitalWrite (ledpin1, HIGH);
  }
  if (z_button == z_buttonmin)
{
  digitalWrite (ledpin3, HIGH);
}
else
{
  digitalWrite (ledpin3, LOW);
}
if (c_button == c_buttonmin)
{
  digitalWrite (ledpin4, HIGH);
}
else
{
digitalWrite (ledpin4, LOW);
}
 
 delay (1000);   
  Serial.print (joy_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (joy_y_axis, DEC);
  Serial.print ("\t");

//  Serial.print (accel_x_axis, DEC);
//  Serial.print ("\t");
//
//  Serial.print (accel_y_axis, DEC);
//  Serial.print ("\t");
//
//  Serial.print (accel_z_axis, DEC);
//  Serial.print ("\t");

  Serial.print (z_button, DEC);
  Serial.print ("\t");

  Serial.print (c_button, DEC);
  Serial.print ("\t");

  Serial.print ("\r\n");
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char
nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Might be easier if you use the WiiChuck library:  http://www.arduino.cc/playground/Main/WiiChuckClass

For ESC control you would typically use the standard Servo library.  The servo.writeMicroseconds() will give you better resolution (range is about 1000 to 2000 microsecond pulse width).  If you are talking "PWM" and "6%" then you probably don't understand how hobby servos are controlled.  That might be a good topic of research.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had tried all those libraries and they didn't work.  I think they are old and need to be updated.   OR I could have been doing something wrong.( I am guessing I didn't do something write).  Anyway using the servo library is what I was going to use.  and the PWM is is the 6% that the scope seen.  I need to get the millaseconds.  I was in a hurry and didn't get a chance to get all of my test that accurate.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know the ESC is controlled with PWM.  Now getting the arduino to read what I want from the nunchuch and have the ESC do what I want is were my problem lies.  Today I am going to try to get some of the other codes to work for the nunchuck.  I think they will help me better if I can get them to work
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK john here is what I have.

The input coming in I can see and now what I need to do is figure out how to get the output of pins 9 and 10 for pulse width modulation.  and I would like to set my limits.  Then make it so that outputs 2 and 4 cannot be turn on at the same time.  Is possible that is in the code already.  I have tried the wii chuck library's and they are not working.  I don't know enough to change them to make them work.  I know the bare minimum to be dangerous.
To make things easier would it be easier to make the forward position to be fixed and when the x joystick is met to the min threshold then one of the motors will shut off and the other will come on.  Instead of trying to make then speed controllers.  just make it a fixed speed.  That I could change with the PWM if I need too.
Logged

Pages: [1]   Go Up
Jump to: