Jesus’s Code

Jesus is responsible for the movement of the two linear stages. His code is written in both Matlab and C++ due to the use of an Arduino as a GPIO interface.

This code block establishes a method of moving the two linear stages which are used to move the RX antenna. This was written in the Arduino IDE.

// We need 316.5 pulses per step for every millimiter 
// we need 1978 pulses per steps to get a total of 400 mm
//we need 64 steps to get the total 400 mm
// there needs to be 64 steps of 2038 pulses per step to cover the entire stage
// if you are all the way to one corner, you need 32 steps of 2038 pulses per step to get right at the middle 
// or one step of 65,216 to get to the middle of the stages  

long int direc = 0; //initializing the serial variable for direction
long int pulsesperstep = 0; // Legth of step
uint8_t first8 = 0;
uint8_t second8 = 0;
uint16_t full = 0;

  void setup() {
    pinMode(4, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(6, OUTPUT);
    Serial.begin(9600);
    Serial.println("Ready"); 
  
}
  
  void loop() { //in this loop Arduino will just wait for a command sent from MATLAB
                //via serial port to do a certain action
    if (Serial.available() >0){
      
      union stepdata{
        uint16_t full;
        struct{
          uint8_t first8;
          uint8_t second8;
        };
      };
      union stepdata stepdata1;
        
      stepdata1.first8 = Serial.read();   
      stepdata1.second8 = Serial.read();
      Serial.println(stepdata1.full);
      long int pulsesperstep = stepdata1.full; // Legth of step
      //delayMicroseconds(1000);
      if (pulsesperstep == 64000){
        digitalWrite(4,HIGH); 
        delay(200);
        digitalWrite(4,LOW);
        delay(200);
        digitalWrite(4,HIGH);
        delay(200);
        digitalWrite(4,LOW);
      }
      long direc = Serial.read(); //Reading and capturing the serial variable for direction in one byte sent from MATLAB
                                      // depending on the value sent Arduino will do a certain function
      Serial.println(direc);
      if (direc == 1) { 
        up();
      }
    
      if (direc == 2) {
        down();
      }
    
      if (direc == 3) {
        right();
      }
    
      if (direc == 4) {
        left();
      }
    }
  }
  
  // Functions for the directions where the stages will move
  void up() {
    digitalWrite(7, LOW);
    // Spin the stepper motor 1 revolution slowly:
    for (long int i = 0; i < pulsesperstep; i++) {
    // These four lines result in 1 step:
    digitalWrite(6, HIGH);
    delayMicroseconds(100);
    digitalWrite(6, LOW);
    delayMicroseconds(100);
    }
    delay(100);
  }
  
  void down() {
    digitalWrite(7, HIGH);
    // Spin the stepper motor 1 revolution slowly:
    for (long int i = 0; i < pulsesperstep; i++) {
    // These four lines result in 1 step:
    digitalWrite(6, HIGH);
    delayMicroseconds(100);
    digitalWrite(6, LOW);
    delayMicroseconds(100);
    }
    delay(100);
  }
  
  void right(){
    digitalWrite(4, HIGH);
    // Spin the stepper motor 1 revolution slowly:
    for (long int i = 0; i < pulsesperstep; i++) {
    // These four lines result in 1 step:
    digitalWrite(3, HIGH);
    delayMicroseconds(100);
    digitalWrite(3, LOW);
    delayMicroseconds(100);
    }
    delay(100);
  }
  
  void left(){
    digitalWrite(4, LOW);
    // Spin the stepper motor 1 revolution slowly:
    for (long int i = 0; i < pulsesperstep; i++) {
    // These four lines result in 1 step:
    digitalWrite(3, HIGH);
    delayMicroseconds(100);
    digitalWrite(3, LOW);
    delayMicroseconds(100);
    }
    delay(100);
  }

This code block establishes a serial communication link between the code that is running on the Arduino and the main program which is written in Matlab.

clear
clc
a=serial('COM3','BaudRate',9600);
fopen(a);
readData=fscanf(a) %reads "Ready" 

%This command will send values up to 65000 for the distance 
%the stages need to move
distance=typecast(uint16(64000),'uint8');
fwrite(a,distance,'uint8') %write data

pause(1);
% For writedata:
% 1 = up, 2 = down, 3 = right, 4 = left.
direction=int16(2); 
fwrite(a,direction,'int16') %write data

readData2=fscanf(a)
readData3=fscanf(a)

%fprintf(readData2);

fclose(a);
delete(a);

This is the code block is a function called stages_function. It takes in as parameters a direction and a distance. It is a move relative function that is the main means of moving the stages.

function stages_function(dist, dir)
%
%   SAT FSS
%
%   This function takes two inputs, pulse length and direction. The 
%   function then takes the two input parameters and moves the stages 
%   as desired by the input. The input will be fed by the test sight 
%   matrix coordinates.
%
%   Author: Jesus Gonzalez
%
%   31 March 2021
%
%   Team Members: Joshua Paine & Daniel Greisen


    %dist uint16
    %dir uint8
    
    a = serial('COM7', 'BaudRate', 9600);
    fopen(a);
    readData = fscanf(a)
    
    dist1 = typecast(uint16(dist),'uint8');
    fwrite(a, dist1, 'uint8')
    fwrite(a, dir, 'uint8')
    
    
    fclose(a);
    delete(a);

end