Hi everyone and sorry for my poor english. I am working on a camera scanning system. The camera will move row by row in a fixed pattern and capture photos at certain positions repeatedly, attached is the simplified moving pattern of the camera.
Basically the flow is arduino controls the motor, and tells computer(python) to capture photos once it reached the position.
Hardware:
arduino mega
M542 step motor driver
And here is the code i use
//motor and dir pin setting
const int mo_x = 2;
const int dir_x = 3;
const int mo_y = 4;
const int dir_y = 5;
//end switch pin
const int endpin_x = 9;
const int endpin_y = 10;
//fixed step
const int xy_step = 2504;
//end switch val for checking
int end_x = 0;
int end_y = 0;
int stage = 0;
boolean initial_done = false; //check for initialized
void setup()
{
Serial.begin(115200);
pinMode(mo_x, OUTPUT);
pinMode(dir_x, OUTPUT);
pinMode(mo_y, OUTPUT);
pinMode(dir_y, OUTPUT);
pinMode(endpin_x, INPUT);
pinMode(endpin_y, INPUT);
delay(3000); //wait for serial connection
}
void loop()
{
switch (stage){
case 0 : // initialise position
while (initial_done == false){
check_end();
while(end_y != HIGH) { // check y end first
back();
move_mo(mo_y);
check_end(); }
while(end_x != HIGH) { // check x end
left();
move_mo(mo_x);
check_end(); }
if (end_x == HIGH && end_y == HIGH ){
initial_done = true;
stage = 1; }
}
break;
case 1 : //Start scanning
if (initial_done == true){
//move to first row
delay(1000);
front();
for (int i = 0; i < xy_step; i++){
move_mo(mo_y); }
for(int row = 1; row <= 5 ;){ // 5 row
//check row even or odd
if ((row %2) != 0){
right();}
else if ((row %2) == 0){
left(); }
// capture
cap();
// move column 5 times
for(int col = 1; col <= 5; col++){
for (int i = 0; i < xy_step; i++){
move_mo(mo_x); }
//capture
cap();
}
//move to next row
if (row != 5 && row <6 ){
for (int i = 0; i < xy_step; i++){
move_mo(mo_y); }
}
if (row == 5){ // go back to start position
check_end();
stage = 0;
initial_done = false; }
row++;
}
}
break;
// func to set dir
void left(){digitalWrite(dir_x, HIGH);}
void right(){digitalWrite(dir_x, LOW);}
void front(){digitalWrite(dir_y, HIGH);}
void back(){digitalWrite(dir_y, LOW);}
// func to move motor
void move_mo(int motor){
digitalWrite(motor, HIGH);
delay(2);
digitalWrite(motor, LOW);
delay(2); }
//func to check cap
void cap(){
delay(2000);
Serial.print("c");
delay(1000);
Serial.flush();
while(!Serial.available()) {}
//check for serial
int incoming;
while (incoming != '1'){
if (Serial.available() >0){
incoming = Serial.read(); }
}
delay(2000);
}
void check_end(){ //func to read end switch state
end_x = digitalRead(endpin_x);
end_y = digitalRead(endpin_y);}
The camera moves in a pattern expected, and it captures photos properly.
But there is a problem, the photos seems shifted after every round of scanning, and i found out that the motor will move a larger step every time it moves instead of the value i set, eventually it will reached the end of the track.
Anyone knows where the problem is?
Many thanks.
