Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: softwareserial cant read on: February 01, 2011, 11:03:26 am
I'd suggest using two non-PWM pins for the software serial port.

i change it to 12&13, still cant get it works smiley-cry
simple code like this is work:
Quote
void loop()
{
      robot_turn(false,90);

      Serial.print((byte)0x2A);
      Serial.print((byte)0x50);
      
        for(int i=0;i<11;i++)
        {
          compass_data = Serial.read();
          if(i>2&&i<6)
          {
            angle[i-3] = compass_data;
          }
        }
        angle_2 = atoi(angle);
        Serial.println(angle_2);
}

 i just cant make it works when i call compass_check() in cal_path(). Nothing special in cal_path(), just have about 160 lines of codes to calculate the degree the robot need to turn, which are not related to any interrupt/PWM/hardware or software serial communication, it's just math. then i check whether the degree is positive or negative, which decides which way should the robot turn. Then i call compass_check to check whether the robot turn too much, or not enough.Finally, i told it to move forward.
2  Using Arduino / Programming Questions / Re: softwareserial cant read on: February 01, 2011, 09:37:24 am
Code:
SoftwareSerial compass(8,9);
Quote
i use 5,6,7,8 pins for the two servo motors
I see a conflict here.

:)thx
I change the pins from 8,9 to 10,11 now, but the program still stop at compass.read() after the motors move smiley-roll-blue.
3  Using Arduino / Programming Questions / Re: softwareserial cant read on: February 01, 2011, 09:20:41 am
Am I missing a compass.begin() statement somewhere?

What pins are the motors using? What kind of motors are they?

i use 5,6,7,8 pins for the two servo motors
and i'm using this motor drive:
http://www.droboticsonline.com/index.php/arduino-motor-drive-shield.html

4  Using Arduino / Programming Questions / Re: softwareserial cant read on: February 01, 2011, 08:28:32 am
The standard software serial is purely polling on receive, so if it isn't listening at the right time it misses stuff .  You'd be a lot better using newsoftserial :  http://arduiniana.org/libraries/newsoftserial/  which does interrupt listening.



i tried newsoftseria b4, the motors counldnt move.
i put serial.print at differnent position and tried to debug it
i found that the program cant even reach the first line of cal_path() when i used newsoftserial
5  Using Arduino / Programming Questions / softwareserial cant read on: February 01, 2011, 04:51:39 am
i am using softwareserial to read data from electronic compass, and i cant use the hardware serial which is connected to other device.

lets show u guys my program first:
Quote
#include <SoftwareSerial.h>

SoftwareSerial compass(8,9);

//other variable

void setup()
{
  attachInterrupt(0, count_pulse_left, RISING);
  attachInterrupt(1, count_pulse_right, RISING);
  pinMode(8,INPUT);
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);
  compass.begin(9600);
  val_x[3] = '\0';
  val_y[3] = '\0';
  angle[3] = '\0';
}

void loop()
{
     Serial.begin(115200);
   //some codes here for the hardware serial communication

   cal_path();
}

void motor1(int high_low2, int pwm2)
{
  digitalWrite(EN2,high_low2);
  analogWrite(IN2,pwm2);
}

void motor2(int high_low1, int pwm1)
{
  digitalWrite(EN1,high_low1);
  analogWrite(IN1,pwm1);
}

void count_pulse_left()
{
  encoder0count++;

}

void count_pulse_right()
{
  encoder1count++;
}

void cal_path()
{
  //some calculations here

 robot_turn(); //control how much the robot should turn
  
  compass_check();
  
  robot_forward(); //move the robot forward

//and both robot_turn() and robot_forward call motor1()/motor2() to control the robot
 }

void compass_check()
{
      compass.print((byte)0x2A); //
      compass.print((byte)0x50); //  acquire data from electronic compass
  
        for(int i=0;i<11;i++)         // to acquire an angle reading from the compass,  11 bytes of data is obtained from the compass
        {
          compass_data = compass.read();

          if(i>2&&i<6)
          {
            angle[i-3] = compass_data;  // the angle reading is at the 4th - 6th byte
          }
          delay(1);
        }
        angle_2 = atoi(angle);

        Serial.print(angle_2);
}

the problem is after the robot turns, angle_2 doesnt shows on the serial monitor, and the robot doesnt move forward.
if i comment robot_turn(); and robot_forward(); ,the program works well and keeps showing angle_2 on the serial monitor.
it seems that if motor1()/motor2() runs before compass.read(), the program just stop, its my observation when i modify the program like this:

Quote
void compass_check()
{
      compass.print((byte)0x2A); //
      compass.print((byte)0x50); //  acquire data from electronic compass

      Serial.print("test");
  
        for(int i=0;i<11;i++)
        {

          compass_data = compass.read();

          if(i>2&&i<6)
          {
            angle[i-3] = compass_data;
          }
          delay(1);
        }
        angle_2 = atoi(angle);

        Serial.print(angle_2);
}

"test" is shown on the serial monitor but angle_2 is not shown.

wt makes me more confused is that the following program works...

Quote
//All the variables, sub-functions are the same as the above program

void loop()
{   Serial.begin(115200);

          motor1(LOW,180);
          delay(1000);
          motor1(LOW,0);
  
  compass_check();

  motor1(LOW,180);
  motor2(LOW,180);
  delay(1000);
  
  delay(100);
}


everytime it finishs turning, it shows angle_2 on the serial monitor, and then move forward.

so why the softwareserial doesnt work(cant read) in the first program(which is a very long program, about 500 lines, but i guess it has nothing to do with the length of the program)?
6  Forum 2005-2010 (read only) / Troubleshooting / Re: closed loop dc motors control on: December 29, 2010, 12:15:18 am
Quote
http://www.arduino.cc/playground/Main/RotaryEncoders
Typically, encoders have a PinA and a PinB. They are connected to two pins on the Arduino.

So, it appears that this code is reading one encoder. How that gives you any information about the difference between two encoders is beyond me.
I'm sorry that I've made a mistake, the code in my last reply just measures the encoder position of one encoder, however, when the wheels move in one direction, sometimes the encoder position increases, sometimes it decreases, so I thought reading two pins may cause error and I only measured one pin instead.
7  Forum 2005-2010 (read only) / Troubleshooting / Re: closed loop dc motors control on: December 23, 2010, 11:54:54 pm
the circumferences of the two wheels should be the same and the ratio should be close to 1.

If using PID control, can i converge on the difference between rpm between the wheels?

To determine the phrase between the two wheels, i have used a program to test:

Quote
int val;
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;

 void setup() {
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
 }

 void loop() {
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
     Serial.print (encoder0Pos);
     Serial.print ("/");
   }
   encoder0PinALast = n;
 }

However, when the wheels move in one direction, sometimes the encoder position increases, sometimes it decreases. If interrupt is used, will serial output fast enough to read the pulse? If not, are there any other ways to read the pulse in order to do the speed control.
8  Forum 2005-2010 (read only) / Troubleshooting / Re: closed loop dc motors control on: December 23, 2010, 08:06:24 am
[size=14]thx for your reply, I am using Arduino Duemilanove and i just focus on making the vehicle moving in a straight line, no other tasks are performed by the Arduino.
And the enocders i use are a optical shaft encoder attached to the motors, it generates 512 pulse per rotation, and it has two channel which has 90 degree pharse difference with each other. But i found that the two channel are actually in pharse in some testing i did before.(maybe i made mistake, i'll try to check the pharse differnece between them), so i drop one channel...
What should i do if the left progress is not the same with the right progress? Does PID control help? How to measure the
error term - difference between left progress and right progress but not the difference between the RPM of the two motors?[/size]

9  Forum 2005-2010 (read only) / Troubleshooting / closed loop dc motors control on: December 23, 2010, 03:35:45 am
Hi all,

I am trying to use the arduino board with the motor shield to control the speed of the motor. I have tried to control the motor speed using the following command:

Quote
void motor1(int high_low1, int pwm1)
{
 digitalWrite(EN1,high_low1);
 analogWrite(IN1,pwm1);
}

void motor2(int high_low2, int pwm2)
{
 digitalWrite(EN2,high_low2);
 analogWrite(IN2,pwm2);
}

void loop()
{
  motor1(high, 180);
  motor2(high, 180);
}


However, when i test the motor with the vehicle, the vehicle cannot move straightly. Then i tried to use a motor embedded with an encoder to do the dc motor feedback control. The speed of the left motor is kept constant while the speed of the right motor is changed. If the pulse count on the left motor is larger, the speed of the right motor increases. If the pulse count on the right motor is larger, the speed of the right motor decreases. However, this program also doesn't work as after some time speed of the right motor will keep increasing or keep decreasing.


Quote
int IN1 = 6;
int EN1 = 7;
int IN2 = 5;
int EN2 = 4;
int encoder0count = 0;       //encoder for left motor
int encoder1count = 0;       //encoder for right motor
int encoder0PinA = 2;
int encoder1PinA = 8;
int encoder0PinALast = LOW;
int encoder1PinALast = LOW;
int n0 = LOW;
int n1 = LOW;
int l_pwm = 180;        //speed of left motor
int r_pwm = 180;       // speed of right motor
int timer = 0;

void setup()
{
  Serial.begin(115200);
}

void loop()
{

  motor1(HIGH,l_pwm);
  motor2(HIGH,r_pwm);

  n0 = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n0 == HIGH)) {

    encoder0count++;
  }
  encoder0PinALast = n0;

  n1 = digitalRead(encoder1PinA);
  if ((encoder1PinALast == LOW) && (n1 == HIGH)) {

    encoder1count++;
  }
  encoder1PinALast = n1;

  Serial.print(encoder0count);
  Serial.print(" ");
  Serial.print(encoder1count);
  Serial.print(" ");
  Serial.print(l_pwm);
  Serial.print(" ");
  Serial.print(r_pwm);
  Serial.println(" ");

  if (timer ==100)
  {

    if (encoder0count < encoder1count)
    {
      r_pwm--;
    }

    else if (encoder0count > encoder1count)
    {
      r_pwm++;
    }

    encoder0count = 0;
    encoder1count = 0;
    timer = 0;

  }

  timer++;
}

void motor1(int high_low1, int pwm1)
{
  digitalWrite(EN1,high_low1);
  analogWrite(IN1,pwm1);
}

void motor2(int high_low2, int pwm2)
{
  digitalWrite(EN2,high_low2);
  analogWrite(IN2,pwm2);
}


I want to ask whether there are something wrong in the program or there are any other ways to control the motor speed so the vehicle can move straight.
10  Forum 2005-2010 (read only) / Troubleshooting / Re: Problem using arduino board to write data on: November 19, 2010, 11:05:06 am
thanks u guys, i'll try again later smiley
11  Forum 2005-2010 (read only) / Troubleshooting / Re: Problem using arduino board to write data on: November 18, 2010, 12:00:33 pm
I have tried adding 1 second delay and 5 seconds delay in the program, but there is still problem.
12  Forum 2005-2010 (read only) / Troubleshooting / Problem using arduino board to write data on: November 17, 2010, 11:18:14 pm
Hi all
I encounter problems while writing data using arduino board. I have written a simple progam for testing as followed:

Quote
void setup()
{
   Serial.begin(115200);
}

void loop()
{
   Serial.write(0xAA);
}


I use the max232 to convert the voltage level to rs232, and use the hyperterminal to observe the output. However, from the hyperterminal there are many "0" inserted to the output. How can these be solved?
Thanks.
13  Forum 2005-2010 (read only) / Troubleshooting / Re: Problem about receiving data on: September 16, 2010, 07:41:55 am
Thanks Groove!
after adding a delay when reading data, i have solved the problem ;D
14  Forum 2005-2010 (read only) / Troubleshooting / Problem about receiving data on: September 15, 2010, 11:37:01 pm
Hi all,
I have written a program using Arduino Duemilanove to receive data (integers ranging from 0 - 500) sent from a computer via a wifi module. However, there are some problems . I open the serial monitor to observer the data received. However, i observed that sometimes the data received is correct, sometimes the data become  -1.

The program are quoted below. There are some bytes added to the data for sending. The length of the data is at the 4th byte, the data sent will be starting at the 6th byte. For example, if the data is a 1 digit integer, the data is at the 6th byte. If the data is a 2 digit integer, the data is at the 6th byte and 7th byte etc. In the program, the data length (the 4th byte) will be read first.

If the data length is 1, the ascill code of the integer will be stored at variable val_z, If the data length is 2, the ascill code of the  units digit will be stored at variable val_z nd that of the tens digit will be stored at variable val_y. If the data length is 3, the ascill code of the units digit will be stored at variable val_z nd that of the tens digit will be stored at variable val_y and the hundred digit will be stored at variable val_x.


Quote
int val_x,val_y,val_z;
int data_length;


void setup()
{
 }

void loop()
{
  Serial.begin(115200);
  
  if(Serial.available()>0)
  {
    for(int i=0;i<4;i++)
    {
        data_length = Serial.read();
    }
    
    for(int j=0;j<data_length+1;j++)
    {
      val_z = Serial.read();
      
      if(data_length==1)
      {
        val_y = 0;
        val_x = 0;
      }
      
      if(data_length==2)
      {
        if(j==1) val_y = val_z;
        val_x = 0;
      }
      
      if(data_length==3)
      {
        if(j==1) val_x = val_z;
        if(j==2) val_y = val_z;
      }
    
    }
    
    Serial.println(val_x);
    Serial.println(val_y);
    Serial.println(val_z);
    
    Serial.flush();
    Serial.end();
    
  }

}

Can anyone help?
15  Forum 2005-2010 (read only) / Interfacing / Re: communicate with wifi module on: September 07, 2010, 03:39:17 am
thanks Grumpy_Mike!!!
it works!!
 
Pages: [1] 2