Etape 5: programmation
Beaucoup de caméras qui est exploités par RS232 signalisation utiliser les commandes hexadécimales. Ces commandes sont envoyées à la caméra comme une série d’octets hexadécimaux. Un instantané du manuel qui répertorie quelques-unes des commandes est montré ci-dessus.
Un tableau d’octets hexadécimal est créé pour chaque commande que vous souhaitez utiliser. Lorsque vous utilisez la commande, il est important de faire correspondre la vitesse de transmission de votre appareil et envoyer chaque octet dans le tableau dans l’ordre. Dans le manuel on voit que le débit en bauds est 9600 bps de sorte que c’est ce qui a été spécifié dans le code. Il est aussi important de spécifier les ports que vous utiliserez pour TX et RX. Pour cela, lorsque vous configurez un SoftwareSerial.
Cet exemple de code ci-dessous a la caméra alternant entre 2 positions prédéfinies toutes les cinq secondes.
// Serial Communication // October 29, 2015 // Purpose: Control the camera position using RS232 communications through a DB9 connector // with an attached RS232 Shield. // Status: WORKING#include <SoftwareSerial.h>// Do not use 0 and 1 if you want to be able to upload code while shield is attached to Arduino SoftwareSerial mySerial(2, 3); //2 is TX, 3 is RX // Camera Commands // Default message 8x 01 04 3F 02 0p FF where x is camera number and p is preset number byte preset1[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x01, 0xFF}; // Camera 1 preset byte preset2[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x02, 0xFF}; // Camera 2 presetbyte address_command[4] = {0x88, 0x30, 0x01, 0xFF}; // Sets camera address (Needed for Daisy Chaining) byte if_clear[5] = {0x88, 0x01, 0x00, 0x01, 0xFF}; // Checks to see if communication line is clearint delayTime = 500; //Time between commandsvoid setup() { Serial.begin(9600); // Serial Communication w/ Computer for Debugging mySerial.begin(9600); // Serial Communication w/ Camera //Send Address command for (int i = 0; i < 4; i++) { mySerial.write(address_command[i]); Serial.println("Address Byte Sent"); } delay(delayTime); //delay to allow camera time for next command //Send IF_clear command for (int i = 0; i < 5; i++) { mySerial.write(if_clear[i]); Serial.println("If Clear Byte Sent"); } }void loop() { for (int i = 0; i < (sizeof(preset1)); i++) // sizeof returns a count of bytes, not numbers if using int, float, or long { mySerial.write(preset1[i]); // sends each byte sequentially Serial.println("Camera Position 1 Byte Sent"); } delay(5000); // Waits for 5 seconds for (int i = 0; i < (sizeof(preset2)); i++) // sizeof returns a count of bytes, not numbers if using int, float, or long { mySerial.write(preset2[i]); // sends each byte sequentially Serial.println("Set Camera Position 2 Byte Sent"); } delay(5000); }
Le deuxième exemple de code permet de contrôler la direction dans laquelle les poêlons de l’appareil photo en lui envoyant des commandes par le biais de la fenêtre de terminal sur votre ordinateur.
// Serial Communication // October 30, 2015 // Purpose: Control the camera position using keyboard inputs // Status: WORKING#include <SoftwareSerial.h>// Do not use 0 and 1 if you want to be able to upload code while shield is attached to Arduino SoftwareSerial mySerial(2, 3); //2 is TX, 3 is RX // Camera Commands // Default message 8x 01 04 3F 02 0p FF where x is camera number and p is preset number byte preset1[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x01, 0xFF}; // Camera 1 preset byte preset2[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x02, 0xFF}; // Camera 2 preset// Camera Controls byte panLeft[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x01, 0x03, 0xFF}; // Pan Camera Left byte panRight[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x02, 0x03, 0xFF}; // Pan Camera Right byte panUp[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x01, 0xFF}; // Pan Camera Up byte panDown[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x02, 0xFF}; // Camera Pan Down byte panStop[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x03, 0xFF}; // Camera Pan Downbyte address_command[4] = {0x88, 0x30, 0x01, 0xFF}; // Sets camera address (Needed for Daisy Chaining) byte if_clear[5] = {0x88, 0x01, 0x00, 0x01, 0xFF}; // Checks to see if communication line is clearint delayTime = 500; //Time between commandsvoid setup() { Serial.begin(9600); // Serial Communication w/ Computer for Debugging mySerial.begin(9600); // Serial Communication w/ Camera //Send Address command for (int i = 0; i < 4; i++) { mySerial.write(address_command[i]); } delay(delayTime); //delay to allow camera time for next command //Send IF_clear command for (int i = 0; i < 5; i++) { mySerial.write(if_clear[i]); } Serial.print("Please Input Commands"); }void loop() { // Checks for serial input from terminal if (Serial.available() > 0) { char inChar = Serial.read(); // read incoming serial data: if (inChar == '1') // Camera Preset 1 { for (int i = 0; i < (sizeof(preset1)); i++) { mySerial.write(preset1[i]); // sends each byte sequentially } } else if (inChar == '2') // Camera Preset 1 { for (int i = 0; i < (sizeof(preset2)); i++) { mySerial.write(preset2[i]); } } else if (inChar == 'w') // Pan Camera Up { for (int i = 0; i < (sizeof(panUp)); i++) { mySerial.write(panUp[i]); } } else if (inChar == 'a') // Pan Camera Left { for (int i = 0; i < (sizeof(panLeft)); i++) { mySerial.write(panLeft[i]); } } else if (inChar == 's') // Pan Camera Down { for (int i = 0; i < (sizeof(panDown)); i++) { mySerial.write(panDown[i]); } } else if (inChar == 'd') // Pan Camera Right { for (int i = 0; i < (sizeof(panRight)); i++) { mySerial.write(panRight[i]); } } else if (inChar == 'q') // Stop Camera Pan { for (int i = 0; i < (sizeof(panStop)); i++) { mySerial.write(panStop[i]); } } } }