Front page   Edit Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes

Diff of Camera Platform Control using Acceleration Sensor

Top / Camera Platform Control using Acceleration Sensor

#contents

** Movie [#vc271ecf]
#youtube(Fd2K8ieo5xQ)

** Devices [#df8a0bb4]
*** Arduino Board [#lb0578f9]

*** Acceleration Sensor [#p9b53043]
[[KXM52-1050:http://akizukidenshi.com/catalog/g/gI-01425/]]

*** CCD Camera [#hcf82b2e]
[[Keyence CK-300:http://hobby.keyence.co.jp/ccd/ck300/index.html]]

*** USB Video Capture [#yd035d79]
[[Princeton USB Video Capture Unit PCA-DAV:http://www.amazon.co.jp/%E3%83%97%E3%83%AA%E3%83%B3%E3%82%B9%E3%83%88%E3%83%B3%E3%83%86%E3%82%AF%E3%83%8E%E3%83%AD%E3%82%B8%E3%83%BC-Princeton-USB%E3%83%93%E3%83%87%E3%82%AA%E3%82%AD%E3%83%A3%E3%83%97%E3%83%81%E3%83%A3%E3%83%BC%E3%83%A6%E3%83%8B%E3%83%83%E3%83%88-%E3%83%87%E3%82%B8%E9%80%A0-PCA-DAV/dp/B000LMPDCM/ref=sr_1_1?ie=UTF8&qid=1262503764&sr=8-1-fkmr0]]

** Code for Arduino [#n958105b]

Now, you can control RC servos with [[Servo library:http://www.arduino.cc/en/Reference/Servo]]. I recommend using the library instead of the following code.

 /*
    RCServoMotorsControlledWithAccelMeter2
    based on the ITP's sample code
    http://itp.nyu.edu/physcomp/Labs/Servo
 */
 // camera_platform_control1
 
 #define NUM_SERVO	2
 #include <Servo.h>
 
 int servoPin[NUM_SERVO] = { 2, 3 };     // Control pins for servo motors
 int minPulse = 500;                               // Minimum servo position
 int maxPulse = 2500;				// Maximum servo position
 int pulseRange = maxPulse - minPulse;  // Range of the servo position
 int pulseWidth[NUM_SERVO] = { 0, 0 };  // Pulse width for the servo motors
 Servo rcs[2];
 const int rcs_pin[] = { 9, 10 };
 int rcs_pos[] = { 0, 0 };
 
 long lastPulse = 0;    // Time in milliseconds of the last pulse
 int refreshTime = 10;  // Time needed in between pulses
 const int accel_pin[] = { 0, 1, 2 };
 int accel_val[] = { 0, 0, 0 };
 
 int analogPin[NUM_SERVO] = { 0, 1 };   // The analog pin that the sensor's on
 int minSensorValue[NUM_SERVO] = { 320, 290 };  // Minimum sensor value
 int maxSensorValue[NUM_SERVO] = { 760, 740 };  // Maximum sensor value
 int sensorRange[NUM_SERVO] = { maxSensorValue[0] - minSensorValue[0],
                                maxSensorValue[1] - minSensorValue[1] };
                              // Range of the sensor values
 int sensorValue[NUM_SERVO] = { 0, 0 };  // The value returned from the analog sensor
 
 int phase = 0;    // variable to select the servo motor to drive
 
 
 //------------------------------------------------------------------------------
 // convert the pulse width to a range between minPulse and maxPulse
 //------------------------------------------------------------------------------
 int checkPulseWidth(int pulseWidth)
 {
     if (pulseWidth < minPulse) return minPulse;
     if (pulseWidth > maxPulse) return maxPulse;
     return pulseWidth / 10 * 10;
 void setup() {
   for (int i = 0; i < 2; i++) {
     rcs[i].attach(rcs_pin[i]);
   }
 }
 
 //------------------------------------------------------------------------------
 // setup function
 //------------------------------------------------------------------------------
 void setup()
 {
     for (int i = 0; i < NUM_SERVO; i++) {
         // Set servo pin as an output pin
         pinMode(servoPin[i], OUTPUT);
         // Set the motor position value to the minimum
         pulseWidth[i] = minPulse;
     }
     
     Serial.begin(9600);    // Start serial communication
 void loop() {
   for (int i = 0; i < 3; i++) {
     accel_val[i] = analogRead(accel_pin[i]);
   }
   
   rcs[0].write(180 - accel_val[0]*0.18);
   rcs[1].write(accel_val[1]*0.18);
   
   delay(50);
 }
 
 //------------------------------------------------------------------------------
 // loop function
 //------------------------------------------------------------------------------
 void loop()
 {
     for (int i = 0; i < NUM_SERVO; i++) {
         sensorValue[i] = analogRead(analogPin[i]);  // read the analog input
         pulseWidth[i] = (int)((float)(sensorValue[i] - minSensorValue[i]) / 
                               sensorRange[i] * pulseRange) + minPulse;
         pulseWidth[i] = checkPulseWidth(pulseWidth[i]);  // convert the pulse width
     }
     
     // pulse the servo again if the refresh time (20ms) have passed:
     if (millis() - lastPulse >= refreshTime) {
         digitalWrite(servoPin[phase], HIGH);   // Turn the motor on
         delayMicroseconds(pulseWidth[phase]);  // Length of the pulse sets the motor position
         digitalWrite(servoPin[phase], LOW);    // Turn the motor off
         lastPulse = millis();    // Save the time of the last pulse
         
         // display the data
         if (phase == 1) {
             for (int i = 0; i < NUM_SERVO; i++) {
                 Serial.print("SV");
                 Serial.print(i);
                 Serial.print(": ");
                 Serial.print(sensorValue[i]);
                 Serial.print("   ");
                 Serial.print("PW");
                 Serial.print(i);
                 Serial.print(": ");
                 Serial.print(pulseWidth[i]);
                 Serial.print("   ");
             }
             Serial.println();
         }
         
         // update the phase variable
         phase++;
         if (phase > NUM_SERVO-1) phase = 0;
     }
 }