Author Topic: serial communications  (Read 5674 times)

0 Members and 1 Guest are viewing this topic.

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
serial communications
« on: February 26, 2010, 05:43:02 PM »
i have a poloulu reflectance sensor and ive been told that you need to charge the capacitor and then count how long it takes it to discharge it. what would be the code to do this on a arduino.  and how would i beable to display that number on the serial monitor in the arduino software

thanks, CJ

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #1 on: February 26, 2010, 08:54:06 PM »
Pololu QTR Reflectance Sensor Application Note
Information about using the Pololu QTR-xA and QTR-xRC reflectance sensors, including sample oscilloscope screen captures of sensor outputs.

Each QTR-1RC [http://www.pololu.com/catalog/product/959] and
QTR-8RC [http://www.pololu.com/catalog/product/961] reflectance sensor
phototransistor output is tied to a capacitor discharge circuit as shown
on the right, which allows a digital I/O line on a microcontroller to take
an analog reflectance reading by measuring the discharge time of the
capacitor. When you have a microcontroller’s digital I/O connected to a
sensor output, the typical sequence for reading that sensor is:
1. Set the I/O line to an output and drive it high
2. Allow at least 10 us for the 10 nF capacitor to charge
3. Make the I/O line an input (high impedance)
4. Measure the time for the capacitor to discharge by waiting for the
I/O line to go low



A guide to using the Pololu QTRSensors library with Arduinos and Arduino-compatible devices like the Pololu Orangutan robot controllers.
http://www.pololu.com/docs/0J19
http://www.pololu.com/docs/pdf/0J19/QTR_arduino_library.pdf

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #2 on: February 26, 2010, 09:01:53 PM »
1. Set the I/O line to an output and drive it high
2. Allow at least 10 us for the 10 nF capacitor to charge
3. Make the I/O line an input (high impedance)
4. Measure the time for the capacitor to discharge by waiting for the
I/O line to go low
 
i know how to do steps 1-3 but  how do i do 4 and what is a us in 2

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #3 on: February 26, 2010, 09:44:18 PM »
In step 2, 10us is 10 micro-seconds.

Is better and easy to used the Pololu QTRSensors library from Pololu
Follow the guide to install the Lib and here is the code to output to serial monitor
Code: [Select]
#include <PololuQTRSensors.h>  
  
// create an object for your type of sensor (RC or Analog)  
// in this example we have three sensors on analog inputs 0 - 2, a.k.a. digital pins 14 - 16  
PololuQTRSensorsRC qtr((unsigned char[]) {14, 15, 16}, 3);  
// PololuQTRSensorsA qtr((unsigned char[]) {0, 1, 2}, 3);  
  
void setup()  
{  
  // optional: wait for some input from the user, such as  a button press  
  
  // then start calibration phase and move the sensors over both  
  // reflectance extremes they will encounter in your application:  
  int i;  
  for (i = 0; i < 250; i++)  // make the calibration take about 5 seconds  
  {  
    qtr.calibrate();  
    delay(20);  
  }  
  
  // optional: signal that the calibration phase is now over and wait for further  
  // input from the user, such as a button press  
  
  Serial.begin(9600);
}  

void loop()  
{  
  unsigned int sensors[3];  
  // get calibrated sensor values returned in the sensors array, along with the line position  
  // position will range from 0 to 2000, with 1000 corresponding to the line over the middle sensor  
  int position = qtr.readLine(sensors);  
  
// output to Serial monitor
Serial.print(sensors[0]);
Serial.print(" ");
Serial.print(sensors[1]);
Serial.print(" ");
Serial.print(sensors[2]);
Serial.print(" ");
Serial.println();

  // if all three sensors see very low reflectance, take some appropriate action for this situation  
  if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750)  
  {  
    // do something.  Maybe this means we're at the edge of a course or about to fall off a table,  
    // in which case, we might want to stop moving, back up, and turn around.  
    return;  
  }  
  
  // compute our "error" from the line position.  We will make it so that the error is zero when  
  // the middle sensor is over the line, because this is our goal.  Error will range from  
  // -1000 to +1000.  If we have sensor 0 on the left and sensor 2 on the right,  a reading of -1000  
  // means that we see the line on the left and a reading of +1000 means we see the line on  
  // the right.  
  int error = position - 1000;  
  
  int leftMotorSpeed = 100;  
  int rightMotorSpeed = 100;  
  if (error < -500)  // the line is on the left  
    leftMotorSpeed = 0;  // turn left  
  if (error > 500)  // the line is on the right  
    rightMotorSpeed = 0;  // turn right  
  
  // set motor speeds using the two motor speed variables above  
}  



Here is the step 1 to 4 how there do it in the Pololu QTRSensors library

Code: [Select]
// Reads the sensor values into an array. There *MUST* be space
// for as many values as there were sensors specified in the constructor.
// Example usage:
// unsigned int sensor_values[8];
// sensors.read(sensor_values);
// ...
// The values returned are in microseconds and range from 0 to
// timeout_us (as specified in the constructor).
void PololuQTRSensorsRC::readPrivate(unsigned int *sensor_values)
{
unsigned char i;
unsigned char start_time;
unsigned char delta_time;
unsigned int time = 0;

unsigned char last_b = _portBMask;
unsigned char last_c = _portCMask;
unsigned char last_d = _portDMask;

// reset the values
for(i = 0; i < _numSensors; i++)
sensor_values[i] = 0;

// set all sensor pins to outputs
DDRB |= _portBMask;
DDRC |= _portCMask;
DDRD |= _portDMask;

// drive high for 10 us
PORTB |= _portBMask;
PORTC |= _portCMask;
PORTD |= _portDMask;

delayMicroseconds(10);

// set all ports to inputs
DDRB &= ~_portBMask;
DDRC &= ~_portCMask;
DDRD &= ~_portDMask;

// turn off pull ups
PORTB &= ~_portBMask;
PORTC &= ~_portCMask;
PORTD &= ~_portDMask;

unsigned char prevTCCR2A = TCCR2A;
unsigned char prevTCCR2B = TCCR2B;
TCCR2A |= 0x03;
TCCR2B = 0x02; // run timer2 in normal mode at 2.5 MHz
// this is compatible with OrangutanMotors


start_time = TCNT2;
while (time < _maxValue)
{
// Keep track of the total time.
// This explicitly casts the difference to unsigned char, so
// we don't add negative values.
delta_time = TCNT2 - start_time;
time += delta_time;
start_time += delta_time;

// continue immediately if there is no change
if (PINB == last_b && PINC == last_c && PIND == last_d)
continue;

// save the last observed values
last_b = PINB;
last_c = PINC;
last_d = PIND;

// figure out which pins changed
for (i = 0; i < _numSensors; i++)
{
if (sensor_values[i] == 0 && !(*_register[i] & _bitmask[i]))
sensor_values[i] = time;
}
}

TCCR2A = prevTCCR2A;
TCCR2B = prevTCCR2B;
for(i = 0; i < _numSensors; i++)
if (!sensor_values[i])
sensor_values[i] = _maxValue;
}


« Last Edit: February 28, 2010, 10:32:58 PM by billhowl »

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #4 on: February 26, 2010, 10:31:57 PM »
for some reason i cant get the library installed  i put it in documents/arduino/libraries  im on my macbook pro

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #5 on: February 26, 2010, 11:28:20 PM »
you extract it to your arduino-0018/hardware/libraries or

(arduino home folder)/hardware/libraries, and restart the Arduino app.

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #6 on: February 27, 2010, 09:11:09 AM »
on my mac i searched for some of the other libraries that were showing up in the arduino program and i came up with the arduino folder that had the hardware/libraries in it and i saw all the libraries that were showing up in the arduino program . so i put the the pololu library folder in with the rest of them and its still not showing up  ??? ???

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #7 on: February 27, 2010, 04:52:28 PM »
i got the library installed but when i try to compile the first program i get a error: invalid conversion from 'char*' to 'unsigned char*' 
how do i fix this i have absolutely no idea what it means

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #8 on: February 27, 2010, 08:46:58 PM »
May I know which part of the program given this error? or you can show me your code.
You may read this "Type Casting" for more info.

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #9 on: February 27, 2010, 10:36:41 PM »
PololuQTRSensorsRC qtr((char[]) {14, 15, 16}, 3);   it is the code that you gave me

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #10 on: February 27, 2010, 11:14:57 PM »
You can change it to this will do

PololuQTRSensorsRC qtr((unsigned char[]) {14, 15, 16}, 3);

This is take from the demo code, I think it have some bug!  :o

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #11 on: February 28, 2010, 07:05:22 PM »
now is there a way to store the calibration so that i can calibrate it turn off the robot but still save the calibration data

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: serial communications
« Reply #12 on: February 28, 2010, 07:13:22 PM »

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #13 on: February 28, 2010, 07:23:20 PM »
when i use the calibrate function where is the calibrated data stored

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: serial communications
« Reply #14 on: February 28, 2010, 07:48:27 PM »
In the EEPROM. It is non-volatile memory, so even when you power off, the EEPROM data is still there. Write the calibration data to the EEPROM, and you can read it later.

Offline ccdjr106Topic starter

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 0
Re: serial communications
« Reply #15 on: March 01, 2010, 01:06:51 PM »
i understand that but what variable is the calibration data stored in. 

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: serial communications
« Reply #16 on: March 01, 2010, 05:05:02 PM »
Refer the PololuQTRSensors.h here are variables that the calibration data stored
Code: [Select]
// Calibrated minumum and maximum values. These start at 1000 and
// 0, respectively, so that the very first sensor reading will
// update both of them.
//
// The pointers are unallocated until calibrate() is called, and
// then allocated to exactly the size required.  Depending on the
// readMode argument to calibrate, only the On or Off values may
// be allocated, as required.
//
// These variables are made public so that you can use them for
// your own calculations and do things like saving the values to
// EEPROM, performing sanity checking, etc.
unsigned int *calibratedMinimumOn;
unsigned int *calibratedMaximumOn;
unsigned int *calibratedMinimumOff;
unsigned int *calibratedMaximumOff;
Here are the code to output to serial monitor
Code: [Select]
// output to Serial monitor
for(i=0;i<3;i++) //number of Sensors
{
Serial.print(calibratedMaximumOn[i]);
Serial.print(" ");
Serial.print(calibratedMinimumOn[i]);
Serial.print(" ");
Serial.print(calibratedMaximumOff[i]);
Serial.print(" ");
Serial.print(calibratedMinimumOff[i]);
Serial.print(" ");
}



 


Get Your Ad Here

data_list