Show Posts
Pages: [1] 2
1  Using Arduino / General Electronics / Re: Looking to design/ build a pair of H Bridges on: December 05, 2012, 08:26:42 pm
If you are considering using relays, you might instead get two switches like below (one for each motor).

http://www.radioshack.com/product/index.jsp?productId=2062530

Lol, I'm trying to build a robot, I want to electronically control my motors, not physically switch them.
2  Using Arduino / General Electronics / Re: Looking to design/ build a pair of H Bridges on: December 05, 2012, 07:53:55 pm
Quote
because the coil in the relay is going to suck a lot of current isn't it?
Depends on the relay but 20 to 100 mA is not a lot.
Are these typical relay currents? I'm looking at automotive relays because they seem to be whats needed to go up to 15 A (Most seem to be 30 or 40 A).  I am making this up as a go with part selection. Also with regard to controlling a relay, I'm guessing I just connect it to my 12V lead acid battery and switch it with like a 2N2222. Do I need some kind of resistor? Or is the coil resistor typically enough to get the current down? They seem to show coil resistances of like 10-50 Ohm, so I'm kind of raising an eyebrow to this 20-100mA range.
3  Using Arduino / General Electronics / Re: Looking to design/ build a pair of H Bridges on: December 05, 2012, 05:30:06 pm
For a homemade H Bridge of that calibre, you could do it with 2x SPDT relays. If you need PWM control, you need 1 MOSFET between the relays and power source.
This is an interesting option I hadn't considered. Mostly because I have never used relays. Is there anything to really know about them? Working with an relay seems kind of not ideal because the coil in the relay is going to suck a lot of current isn't it?


Also, I found this:

http://www.openservo.com/moin.cgi/Schematic2?action=AttachFile&do=view&target=OpenServo_2.0_032706.pdf

Is this a good approach/ alternative to designing a motor controller? I found it in another thread on these forums and it seems to get around a MOS buffer (or something, I still kind of a wet match in a dark room here). Obviously not using these MOSFETs and I don't really care about the current sensing portion either, just the H bridge (-like) part.
4  Using Arduino / General Electronics / Re: Looking to design/ build a pair of H Bridges on: December 05, 2012, 02:38:40 am
I love you.

Also how did you find these on ebay? When I search H bridge on ebay all I get was a bunch of garbage and L298N's. Thank you so much though!
5  Using Arduino / General Electronics / Re: Looking to design/ build a pair of H Bridges on: December 04, 2012, 08:56:44 pm
At 14-15amp range, you need fairly beefy mosfets and you will need to use a mosfet gate driver for those things at moderate switching frequencies (1k or up).


Why so high of frequencies? I noticed the Pololu and Sabertooth boards mentioned going up to ridiculously high PWM frequencies over 20kHz so you can avoid a high pitched whine, but is the Arduino standard ~500Hz PWM frequency insubstantial? Also I am looking at beefy MOSFETs, like this onehttps://www.jameco.com/Jameco/Products/ProdDS/210542.pdf. Looking at the maximum rating on the drain, 72A peak, 18A continuous I should be fine right?

Just to get some background, you might look through 6 or 15 of the sites linked here,

http://www.google.com/search?num=10&hl=en&site=imghp&tbm=isch&source=hp&biw=994&bih=833&q=h-bridge
I know how to Google lol, and I know what an H bridge is. My questions are more about using MOSFETs. Yes I Googled that and I can't find any good resources on circuit designing or component selection for MOSFETS either. That's why I'm asking here is someone can either explain this kind of stuff or knows of any good resources.
6  Using Arduino / General Electronics / Looking to design/ build a pair of H Bridges on: December 04, 2012, 08:28:45 pm
I'm going to start off by saying I'm a mechanical engineer, so I might need things explained to me as they would be to a child.

I am looking to build two H bridge motor controllers to control both direction and speed for 2 motors with a stall current of 14.5 A, although I expect to be running them much more in the 2-5 A range. I know what an H bridge is and how to operate one, but I am trying to avoid spending a huge grip on something like one of those $50-60 Pololu Dual motor controllers or a $70 Sabertooth Dual motor driver. I also want to learn some new stuff about electrical engineering instead of just buying something.

I understand how an H bridge is supposed to work, especially in the context of a motor with regard to direction, braking, free running, but I have discovered through laughably unsuccessful testing that Darlington Power Transistors are probably the wrong way to go. I'm looking into MOSFETs, of which I know nothing except that they are easier to break (?), but they seem to be able to handle greater currents (despite BJT's being the so called "current driven transistors" and FETs being the "voltage driven transistors"). Anywho, I am looking to control my robot with an Arduino's Digital I/O and PWM pins. My concern is that while looking at datasheets for power MOSFETs on Jameco.com is that all of the threshold voltages show a range of 2-4V with 2.9 typical. Would I kill one of these things with a 5V (or 4.5-4.9V what ever the range is) Arduino signal? Is there a way to bring the Voltage down? I wouldn't really have a clue since the extent of my electrical circuit analysis pretty much stops at analyzing ideal OP Amp circuits and normal, useless, KCL KVL RCL circuit analysis. Does anyone know of any good (free) online guides or resources I could look into for designing moderately high current H bridges, or at least something on using MOSFETs are switches? Any help would be graciously appreciated.

Don't worry so much about the multi direction and speed control part, I'm just trying to figure out what signals to send to a MOSFET and how to condition them from an Arduino.
7  Using Arduino / Motors, Mechanics, and Power / Re: Trying to control 2 12V motors on: October 29, 2012, 03:54:41 pm
Wow. That's awesome. I had no idea you could flicker an H-bridge like that to get direction AND speed control. ME vs Transistors -_- lol.
8  Using Arduino / Motors, Mechanics, and Power / Re: Trying to control 2 12V motors on: October 27, 2012, 02:21:07 pm
But you're saying I could use PWM on my inputs to the L298 to kind of "flicker" the h-bridge for speed control? I had no idea that could be done, but that's really cool.
9  Using Arduino / Motors, Mechanics, and Power / Re: Trying to control 2 12V motors on: October 26, 2012, 06:42:42 pm
How do you do Speed control with a L298? All it has is 4 inputs (which I am coupling to 2) and a voltage supply? I thought you controlled the supply voltage, and I supposed to put PWM values into the H-ridge inputs? I was under the impression those were high/ low and sustained.

What I am doing is basically on the data sheet page 7, but with a TIP120 in between the voltage source and the L298. Can I really flash the L298 H-bridge inputs to control the voltage?
10  Using Arduino / Motors, Mechanics, and Power / Trying to control 2 12V motors on: October 26, 2012, 04:34:29 pm
I'm a mechanical engineering student and my electrical skills leave a lot to be desired, but I'm really racking my brain on this project.

I am making a robot that is driven by 2 independently controlled motors, which makes things easy because I should only need to design 1 circuit twice (and yes, my battery does provide enough power). The circuit I have made doesn't really work, so I wanted to describe my circuit and ask about the importance of order (or maybe just find out my entire circuit is complete garbage). So here goes:

For each motor I have basically a linear circuit that goes from the 12V terminal of my battery to the collector of a TIP120 out of the emitter of the TIP120 and into an L298, and the TIP120's base is connected to a PWM pin from the arduino. I am using an L298 (a dual H-Bridge), despite it's voltage drop, because you can tie the H-Bridges together to get a single H-Bridge that can handle up to 4 A of current (these are pretty high power motors). So motor is connected to the outputs of the H-Bridge (with a wave rectifier, - to ground, + to 12 V, and the two middle pins to each motor output. The H-bridge is connected to digital output pins on the arduino to change direction. And the circuit is connect to ground through the H-Bridge.

When I run this my TIP120 kind of heats up and I only get like 3.5V on my motor. Does order matter for this circuit (should the L298 come before the TIP120?)? The voltage drop for one of the H-bridges on an L298 is like 1-2 volts, by pairing them up should I really expect to lose this much voltage? This may sound dumb too, but do I need a rectifier diode between collector and emitter of the TIP120 (because I thought the rectifier bridge would catch all the feedback if that's what's messing my circuit up). Are there any free circuit drawing programs that I can use to better show my circuit than this little paragraph (its a simple circuit, battery, TIP120, L298, motor on L298, but I can understand how it could be hard to visualize).

So am I going about this wrong? Is this a bad circuit? Or is coupling the h-bridges on a L298 just bad news?

Data sheets
TIP120 http://www.fairchildsemi.com/ds/TI/TIP120.pdf
L298 http://www.sparkfun.com/datasheets/Robotics/L298_H_Bridge.pdf
11  Using Arduino / Motors, Mechanics, and Power / Re: Controlling 2x 24 volts DC, 450 watts motors. Where do i start ? on: September 24, 2012, 05:04:39 pm
Why not design your own shield? I mean what do you need the motors to do? Do you need speed control? Do you need directional control? Whats the maximum current that runs through the motors?

I'm doing a similar project where I need to control 2 12V DC motors that need speed and directional control. All you need is Power transistors and H-Bridges that can handle your max current and you should be good to go. Maybe check some things like voltages drops, admittedly I kind of dropped the ball on that with my project, but like The maximum current on my motors are 4A, but that's only around stall, they typically run around 1-2A, so I have have a TIP120 and an L298 in series for each motor. The power transistor is used as a switch with a PWM value from the arduino on the base. The L298 is actually a dual H-bridge but you can tie em together to act like a single 4 A H-Bridge, and that gives me directional control with digital inputs from the arduino. In my case, the L298 was kind of a poor choice because of the voltage drops and how they propagate when you do what I'm doing with them, but it still works. Best part: You can get all the stuff from Jameco for like $.50-3.00 a part. You will have to make a board, either design a circuit board (Fritzing REALLY helps) or get some perf board with a million jumpers, but it WAY cheaper and not that hard. It does take some elbow grease though and design though. That's kind of what you pay for with those other motor controllers though; you're paying the guy who thought up and designed those break out boards, then you are paying the manufacturing costs. If you're doing a project on an arduino you probably aren't suppe worried about profession quality, time, or convenience, so why not make you're own board? Plus is super rewarding to build your stuff more from scratch than to just kind of buy things and stick them together.
12  Using Arduino / Programming Questions / Re: Lag(?) issues on: September 05, 2012, 08:13:48 pm
Ok I finally have something working. There may still be some bugs (in fact I'm sure I have an incongruity with left and right cases between FSM's, but that's easy enough to fix), but at least I have a program that is working and a robot that I can test so I can fine tune a few things (the steerVal is probably too short, but I'll need to test to see if I need 2x, 5x or something like a square of what I have) but I wanna say think you guys for helping me out. I've learned a lot about getting programs to run in real time and debugging and stuff.

if you're curious with where I was wanting to go or what I ended up with:
Code:
#include <Servo.h>

#define servo_wait 1
#define servo_leftscan 2
#define servo_motionwait 3
#define servo_rightscan 4
#define servo_reset 5

#define sensor_wait 1
#define sensor_sense 2
#define sensor_delay 3
#define sensor_reset 4

#define steering_wait 1
#define steering_detectR 2
#define steering_detectL 3
#define steering_compare 4
#define steering_steer 5
#define steering_loop 6
#define steering_stop 7

Servo servo;

int servoCase = servo_wait;
int sensorCase = sensor_wait;
int steeringCase = steering_wait;

int heading = 90;
const int scan = 15;
const int shortest = 3000;
const int farthest = 9000;
boolean detectR;
boolean detectL;
unsigned long sensorRead;
int steerVal;

int rMot;
int lMot;
int servoPos = 90;
int base = 255;

unsigned long tus;
unsigned long tms;
int n = 1;

int rMotPin = 2;
int lMotPin = 3;
int servoPin = 14;
int sensorPin = 15;
int hBdg1 = 16;
int hBdg2 = 17;
int hBdg3 = 18;
int hBdg4 = 19;

void setup()
{
  Serial.begin(9600);
  servo.attach(servoPin);
  servo.write(servoPos);
  pinMode(rMotPin, OUTPUT);
  pinMode(lMotPin, OUTPUT);
  pinMode(hBdg1, OUTPUT);
  pinMode(hBdg2, OUTPUT);
  pinMode(hBdg3, OUTPUT);
  pinMode(hBdg4, OUTPUT);
  forward();
  servoCase = servo_leftscan;
}

void loop()
{
  //servo FSM
  switch (servoCase)
  {
// Stand-by state for servo FSM   
    case servo_wait:
      break;
// Turns servo left and initiates sensor FSM
    case servo_leftscan:
      servoPos = heading - scan;
      if (servoPos < 0)
      {
        servoPos = 0;
      }
      servo.write(servoPos);
      servoCase = servo_motionwait;
      tms = millis();
      break;
// Creates delay for program that gives the servo .5 seconds to
// reach the desired position with out interrupting the program.
// The delay is to make sure the servo is facing the direction it
// is supposed to before getting reading from the sensor mounted
// on it.
    case servo_motionwait:
      if (millis() - tms >= 500)
      {
        sensorCase = sensor_sense;
        servoCase = servo_wait;
        break;
      }
      break;
// Turns servo right and initates sensor FSM     
    case servo_rightscan:
      servoPos = heading + scan;
      if (servoPos > 180)
      {
        servoPos = 180;
      }
      servo.write(servoPos);
      servoCase = servo_motionwait;
      break;
// Initiates steering FSM and puts the servo FSM on standby
    case servo_reset:
      steeringCase = steering_compare;
      servoCase = servo_wait;
  }
  //Sensor FSM
  switch (sensorCase)
  {
// Stand- by state for sensor FSM
    case sensor_wait:
      break;
// Sensor process: Sends out 5 us digital high signal to initiate
// then reads in the digital high signal sent from the sensor. The
// sensor is an ultra sonic range finder which sends out an
// ultrasonic signal then "listens" for the "echo" and outputs a
// digital high pulse as long as the time it took for the
// ultrasonic signal to travel from the sensor to the target and
// back in us. Experimentation showed the process does not work
// with 100% reliability, so this case allows the sensor 5 "tries"
// to detect the runner within the speficied range. If the runner
// is not detected in the speficied range, the program will go
// into a short delay case as specified by the specifications
// of the sensor. If the runner is detected in the specified range
// the program will throw the reading to the a steering FSM case
// corresponding to the left or right servo position.
    case sensor_sense:
      pinMode(sensorPin, OUTPUT);
      digitalWrite(sensorPin, LOW);
      delayMicroseconds(2);
      digitalWrite(sensorPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(sensorPin, LOW);
      pinMode(sensorPin, INPUT);
      sensorRead = pulseIn(sensorPin, HIGH, 12000);
      Serial.println(sensorRead);
      tms = millis();
      if ((sensorRead < shortest || sensorRead > farthest) && n <=5)
      {
        n += 1;
        sensorCase = sensor_delay;
        break;
      }
      else
      {
        if (servoPos < heading)
        {
          steeringCase = steering_detectR;
        }
        else
        {
          steeringCase = steering_detectL;
        }
        n = 1;
        sensorCase = sensor_wait;
        break;
      }
// Delays the program before reinitiating the sensor with out
// interrupting the program.
    case sensor_delay:
      if (millis()-tms >= 10)
      {
        sensorCase = sensor_sense;
      }
      break;
  }
  //steering FSM
  switch (steeringCase)
  {
// Stand-by case for steering FSM
    case steering_wait:
      break;
// Processes sensor reading from right detectiong into binary.
// Throws steering FSM back into stand by and initiates servo FSM
// to turn right
    case steering_detectR:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectR = true;
      }
      else
      {
        detectR = false;
      }
      steeringCase = steering_wait;
      servoCase = servo_rightscan;
      break;
// Processes sensor reading from left detection into binary.
// Throws steering FSM into stand-by and servo FSM into reset
    case steering_detectL:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectL = true;
      }
      else
      {
        detectL = false;
      }
      steeringCase = steering_wait;
      servoCase = servo_reset;
      break;
// Compares both left and right detections to determine what the
// robots heading should be to follow the runner and puts the
// steering FSM into a state that will actually change the
// direction of the robot. If the runner was not detected at all
// be the steering FSM is thrown into the "stop" state
    case steering_compare:
      tms = millis();
      if (detectR && detectL)
      {
        steeringCase = steering_steer;
      }
      if (!detectR && detectL)
      {
        heading -= scan;
        if (heading < 0)
        {
          heading = 0;
        }
        steeringCase = steering_steer;
      }
      if (detectR && !detectL)
      {
        heading += scan;
        if (heading > 180)
        {
          heading = 180;
        }
        steeringCase = steering_steer;
      }
      if (!detectR && !detectL)
      {
        steeringCase = steering_stop;
      }
      break;
 // Steering is implimented by turning of one of the motors for a
 // short time. Which motor is determined by the heading of the
 // robot and the length of the turn is determined by the
 // ammount the robots heading is off of going striaght forward.
 // The delay is done with another steering FSM case, similiar
 // to the other delays in the other FSM's.
    case steering_steer:
      steerVal = abs(heading-90);
      if(heading > 90)
      {
        rMot = base;
        lMot = 0;
      }
      else
      {
        rMot = 0;
        lMot = base;
      }
      steeringCase = steering_loop;
      break;
// Delay for executing a turn done so that it does not interupt
// the program. When the delay is complete, the steering FSM is
// thrown back into stand by and the servo FSM is reinitated with
// the left servo turn.
    case steering_loop:
      if (millis() - tms > steerVal)
      {
        rMot = base;
        lMot = base;
        servoCase = servo_leftscan;
        steeringCase = steering_wait;
      }
      break;
// Because the runner was not detected, the robot stops itself,
// resets its heading straight forward, throws the steering FSM
// back into stand by, and reinitatiates the servo FSM with the
// left servo turn.
    case steering_stop:
      rMot = 0;
      lMot = 0;
      heading = 90;
      servoCase = servo_leftscan;
      steeringCase = steering_wait;
      break;
  }
  analogWrite(rMotPin, rMot);
  analogWrite(lMotPin, lMot);
  Serial.print(detectR);
  Serial.print(" ");
  Serial.print(detectL);
  Serial.print(" ");
  Serial.println(heading);
}
void forward()
{
  digitalWrite(hBdg1, HIGH);
  digitalWrite(hBdg2, LOW);
  digitalWrite(hBdg3, HIGH);
  digitalWrite(hBdg4, LOW);
}
void reverse()
{
  digitalWrite(hBdg1, LOW);
  digitalWrite(hBdg2, HIGH);
  digitalWrite(hBdg3, LOW);
  digitalWrite(hBdg4, HIGH);
}
13  Using Arduino / Programming Questions / Re: Lag(?) issues on: September 05, 2012, 04:10:50 pm
So now the robot seems to successfully do nothing at all. The good news (I suppose) is that the serial monitor seems to be outputting a steady stream of data instead of large bursts with long pauses, so I guess that means the loop is running more consistently? I have also case names for clarity

Code:
#include <Servo.h>

#define servo_wait 1
#define servo_leftscan 2
#define servo_motionwait 3
#define servo_rightscan 4
#define servo_reset 5

#define sensor_wait 1
#define sensor_sense 2
#define sensor_delay 3
#define sensor_reset 4

#define steering_wait 1
#define steering_detectR 2
#define steering_detectL 3
#define steering_compare 4
#define steering_forward 5
#define steering_left 6
#define steering_right 7
#define steering_stop 8

Servo servo;

int servoCase = servo_wait;
int sensorCase = sensor_wait;
int steeringCase = steering_wait;

int heading = 90;
const int scan = 15;
const int shortest = 3000;
const int farthest = 11000;
boolean detectR;
boolean detectL;
unsigned long sensorRead;
int steerVal;

int rMot;
int lMot;
int servoPos = 90;
int base = 255;

unsigned long tus;
unsigned long tms;
int n = 1;

int rMotPin = 2;
int lMotPin = 3;
int servoPin = 14;
int sensorPin = 15;
int hBdg1 = 16;
int hBdg2 = 17;
int hBdg3 = 18;
int hBdg4 = 19;

void setup()
{
  Serial.begin(9600);
  servo.attach(servoPin);
  servo.write(servoPos);
  pinMode(rMotPin, OUTPUT);
  pinMode(lMotPin, OUTPUT);
  pinMode(hBdg1, OUTPUT);
  pinMode(hBdg2, OUTPUT);
  pinMode(hBdg3, OUTPUT);
  pinMode(hBdg4, OUTPUT);
  forward();
  servoCase = servo_leftscan;
}

void loop()
{
  //servo FSM
  switch (servoCase)
  {
    case servo_wait:
      break;
    case servo_leftscan:
      servoPos = heading - scan;
      if (servoPos < 0)
      {
        servoPos = 0;
      }
      servo.write(servoPos);
      servoCase = servo_motionwait;
      tms = millis();
      break;
    case servo_motionwait:
      if (millis() - tms >= 400)
      {
        sensorCase = sensor_sense;
        servoCase = servo_wait;
        break;
      }
      break;
    case servo_rightscan:
      servoPos = heading + scan;
      if (servoPos > 180)
      {
        servoPos = 180;
      }
      servo.write(servoPos);
      servoCase = servo_motionwait;
      break;
    case servo_reset:
      steeringCase = steering_compare;
      servoCase = servo_wait;
  }
  //Sensor FSM
  switch (sensorCase)
  {
    case sensor_wait:
      break;
    case sensor_sense:
      pinMode(sensorPin, OUTPUT);
      digitalWrite(sensorPin, LOW);
      delayMicroseconds(2);
      digitalWrite(sensorPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(sensorPin, LOW);
      pinMode(sensorPin, INPUT);
      sensorRead = pulseIn(sensorPin, HIGH, 5);
      tms = millis();
      if ((sensorRead < shortest || sensorRead > farthest) && n <=5)
      {
        n += 1;
        sensorCase = sensor_delay;
        break;
      }
      if (servoPos < heading)
      {
        steeringCase = steering_detectR;
      }
      else
      {
        steeringCase = steering_detectL;
      }
      n = 1;
      sensorCase = sensor_wait;
      break;
    case sensor_delay:
      if (tms >= 100)
      {
        sensorCase = sensor_sense;
      }
      break;
    case sensor_reset:
      if (servoPos < heading)
      {
        servoCase = servo_rightscan;
      }
      else
      {
        servoCase = servo_reset;
      }
      break;
  }
  //steering FSM
  switch (steeringCase)
  {
    case steering_wait:
      break;
    case steering_detectR:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectR = true;
      }
      else
      {
        detectR = false;
      }
      steeringCase = steering_wait;
      break;
    case steering_detectL:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectL = true;
      }
      else
      {
        detectL = false;
      }
      steeringCase = steering_wait;
      break;
    case steering_compare:
      steerVal = abs(heading - 90);
      if (detectR && detectL)
      {
        steeringCase = steering_forward;
      }
      if (!detectR && detectL)
      {
        steeringCase = steering_left;
      }
      if (detectR && !detectL)
      {
        steeringCase = steering_right;
      }
      if (!detectR && !detectL)
      {
        steeringCase = steering_stop;
      }
      break;
    case steering_forward:
      rMot = 255;
      lMot = 255;
      servoCase = servo_leftscan;
      steeringCase = steering_wait;
      break;
    case steering_left:
      rMot = base;
      lMot = 0;
      tms = millis();
      if(millis() - tms >= steerVal)
      {
        lMot = base;
        heading += scan;
        if (heading > 180)
        {
          heading = 180;
        }
        servoCase = servo_leftscan;
        steeringCase = steering_wait;
      }
      break;
    case steering_right:
      rMot = 0;
      lMot = 255;
      tms = millis();
      if (millis() - tms > steerVal)
      {
        rMot = base;
        heading -= scan;
        if(heading < 0)
        {
          heading = 0;
        }
        servoCase = servo_leftscan;
        steeringCase = steering_wait;
      }
      break;
    case steering_stop:
      rMot = 0;
      lMot = 0;
      heading = 90;
      servoCase = servo_leftscan;
      steeringCase = steering_wait;
      break;
  }
  analogWrite(rMotPin, rMot);
  analogWrite(lMotPin, lMot);
  Serial.print(detectR);
  Serial.print(" ");
  Serial.print(detectL);
  Serial.print(" ");
  Serial.println(heading);
}
void forward()
{
  digitalWrite(hBdg1, HIGH);
  digitalWrite(hBdg2, LOW);
  digitalWrite(hBdg3, HIGH);
  digitalWrite(hBdg4, LOW);
}
void reverse()
{
  digitalWrite(hBdg1, LOW);
  digitalWrite(hBdg2, HIGH);
  digitalWrite(hBdg3, LOW);
  digitalWrite(hBdg4, HIGH);
}
14  Using Arduino / Programming Questions / Re: Lag(?) issues on: September 05, 2012, 12:19:43 pm
Quote
Your robot will be flying blind while this code executes. A timeout on the pulseIn call would be a good thing.

What do you mean by a time out? like another delay or something? This was based on: http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor.

If I understand the functionality of the device (and the code), the sensor is activated with a 5 us digital high pulse and then the sensor reading is sent back on the same pin as a timed input digital high pulse, the time of which corresponds to the distance. Or is the the pulseIn() command measuring the digital low time until the digital high input comes? I don't think that's the case though. Should I put in like a 2-5 us delay before the sensorRead = pulseIn() part? I have no idea how much time there is between the end of the initiation output digital high pulse and the beginning of the  reading input digital high pulse. Using the code fromt he ling and just the sensor, that did work. I notice there is an error in my sensor switch structure where there is supposed to be a 100 ms delay between each sensing that isn't happening that i found when I was relabeling all of my cases to repost that code (which I will do shortly after testing to see if i still have problems).
15  Using Arduino / Programming Questions / Re: Lag(?) issues on: September 04, 2012, 06:30:39 pm
Alright so I have rewritten my code:

Code:
#include <Servo.h>

Servo servo;

int servoCase = 1;
int sensorCase = 1;
int steeringCase = 1;

int heading = 90;
const int scan = 15;
const int shortest = 3000;
const int farthest = 11000;
boolean detectR;
boolean detectL;
unsigned long sensorRead;
int steerVal;

int rMot;
int lMot;
int servoPos = 90;
int base = 255;

unsigned long tus;
unsigned long tms;
int n = 1;

int rMotPin = 2;
int lMotPin = 3;
int servoPin = 14;
int sensorPin = 15;
int hBdg1 = 16;
int hBdg2 = 17;
int hBdg3 = 18;
int hBdg4 = 19;

void setup()
{
  Serial.begin(9600);
  servo.attach(servoPin);
  servo.write(servoPos);
  pinMode(rMotPin, OUTPUT);
  pinMode(lMotPin, OUTPUT);
  pinMode(hBdg1, OUTPUT);
  pinMode(hBdg2, OUTPUT);
  pinMode(hBdg3, OUTPUT);
  pinMode(hBdg4, OUTPUT);
  forward();
  servoCase = 2;
}

void loop()
{
  //servo FSM
  switch (servoCase)
  {
    case 1:
      break;
    case 2:
      servoPos = heading - scan;
      if (servoPos < 0)
      {
        servoPos = 0;
      }
      servo.write(servoPos);
      servoCase = 3;
      tms = millis();
      break;
    case 3:
      if (millis() - tms >= 200)
      {
        sensorCase = 2;
        servoCase = 1;
        break;
      }
      break;
    case 4:
      servoPos = heading + scan;
      if (servoPos > 180)
      {
        servoPos = 180;
      }
      servo.write(servoPos);
      servoCase = 3;
      break;
    case 5:
      steeringCase = 4;
      servoCase = 1;
  }
  //Sensor FSM
  switch (sensorCase)
  {
    case 1:
      break;
    case 2:
      pinMode(sensorPin, OUTPUT);
      digitalWrite(sensorPin, LOW);
      delayMicroseconds(2);
      digitalWrite(sensorPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(sensorPin, LOW);
      pinMode(sensorPin, INPUT);
      sensorRead = pulseIn(sensorPin, HIGH);
      tms = millis();
      if ((sensorRead < shortest || sensorRead > farthest) && n <=5)
      {
        n += 1;
        sensorCase = 2;
        break;
      }
      if (servoPos < heading)
      {
        steeringCase = 2;
      }
      else
      {
        steeringCase = 3;
      }
      sensorCase = 5;
      n = 1;
      break;
    case 5:
      if (tms >= 100)
      {
        sensorCase = 1;
        if (servoPos < heading)
        {
          servoCase = 4;
        }
        else
        {
          servoCase = 5;
        }
        break;
      }
      break;
  }
  //steering FSM
  switch (steeringCase)
  {
    case 1:
      break;
    case 2:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectR = true;
      }
      else
      {
        detectR = false;
      }
      steeringCase = 1;
      break;
    case 3:
      if (sensorRead > shortest && sensorRead < farthest)
      {
        detectL = true;
      }
      else
      {
        detectL = false;
      }
      steeringCase = 1;
      break;
    case 4:
      steerVal = abs(heading - 90);
      if (detectR && detectL)
      {
        steeringCase = 5;
      }
      if (!detectR && detectL)
      {
        steeringCase = 6;
      }
      if (detectR && !detectL)
      {
        steeringCase = 7;
      }
      if (!detectR && !detectL)
      {
        steeringCase = 8;
      }
      break;
    case 5:
      rMot = 255;
      lMot = 255;
      servoCase = 2;
      steeringCase = 1;
      break;
    case 6:
      rMot = base;
      lMot = 0;
      tms = millis();
      if(millis() - tms >= steerVal)
      {
        lMot = base;
        heading += scan;
        if (heading > 180)
        {
          heading = 180;
        }
        servoCase = 2;
        steeringCase = 1;
      }
      break;
    case 7:
      rMot = 0;
      lMot = 255;
      tms = millis();
      if (millis() - tms > steerVal)
      {
        rMot = base;
        heading -= scan;
        if(heading < 0)
        {
          heading = 0;
        }
        servoCase = 2;
        steeringCase = 1;
      }
      break;
    case 8:
      rMot = 0;
      lMot = 0;
      heading = 90;
      servoCase = 2;
      steeringCase = 1;
      break;
  }
  analogWrite(rMotPin, rMot);
  analogWrite(lMotPin, lMot);
  Serial.print(detectR);
  Serial.print(" ");
  Serial.print(detectL);
  Serial.print(" ");
  Serial.println(millis());
}
void forward()
{
  digitalWrite(hBdg1, HIGH);
  digitalWrite(hBdg2, LOW);
  digitalWrite(hBdg3, HIGH);
  digitalWrite(hBdg4, LOW);
}
void reverse()
{
  digitalWrite(hBdg1, LOW);
  digitalWrite(hBdg2, HIGH);
  digitalWrite(hBdg3, LOW);
  digitalWrite(hBdg4, HIGH);
}

Initially I was still experiencing the same lag issues I was having. When I plug the serial monitor in I seem to get HUGE bursts of information. The robot will stop doing things for a second, the suddenly things will go grazy for about half a second and like 80 loops worth of Serial.print will show up in the serial monitor, which is really odd because the servo the sensor is mounted on will only go a short distance in one direction; there's just no way my code is being executed. Does any one see any specific problems with my code or anything? Am I doing this FSM thing right?

One thing that seemed strange to me was the case 4 of the steering FSM switch structure. Perhaps a switch structure isn't the way this should be happening? Am I asking this arduino to do too much?
Pages: [1] 2