Tuesday, May 31, 2016

Arduino to MATLAB Serial Communication

Please Check Click Here for the Latest Release of the VIPER Blog
It includes a MATLAB Simulink tutorial for the Incremental Rotary Encoder using the Keyes KY-040 module.

31 May 2016

For the VIPER Radio Telescope an angle (angel) encoder is being used to determine the angle at which the telescope is at any point in time. For this we are currently using the Keyes KY-040 Arduino Rotary Encoder on an Arduino Uno using the Arduino IDE to code for rotation from this site, which shows exactly how to wire, upload, quite literally everything. This was found on Mike's blogs that he had directed me to, thanks Mike. 

--------------------------------------------------------------------------------------------------------------------------
------------------------------------ ANGLE ENCODER ARDUINO CODE ------------------------------------
--------------------------------------------------------------------------------------------------------------------------
NOTE: The following code is directly copied from the website linked above.  This is the code for the NON-PUSHBUTTON because we will reset the counter via the Zedboard when that is necessary. 
--------------------------------------------------------------------------------------------------------------------------
//From bildr article: http://bildr.org/2012/08/rotary-encoder-arduino/

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
  Serial.begin (9600);

  pinMode(encoderPin1, INPUT); 
  pinMode(encoderPin2, INPUT);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 
  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);

}

void loop(){ 
  //Do stuff here

  Serial.println(encoderValue);
  delay(1000); //just here to slow down the output, and show it will work  even during a delay
}


void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

  lastEncoded = encoded; //store this value for next time

}
--------------------------------------------------------------------------------------------------------------------------
The only edit that was made to the code employed in the working model is riding the code of the delay(1000); entirely, so that the code will spit out the current value as fast as it can.

To start the small scale testing the serial monitor of the Arduino IDE was used as a check. These values changed at increments of 1 or -1 depending upon rotation and the wiring of the CLK and DT pins. 


Here it goes from 2 to 3, and so on. Initially when I started running the first bits were 0, this must be 0 at the start. You can also get the EXACT location on the computer of the serial feed (COM PORT). In my case the serial feed is "/dev/cu.usbmodem1411". When this address (COM PORT) is entered into MATLAB it will be changed slightly to ('/dev/tty.usbmodem1411'). In MATLAB the ' ' must go around the location of your COM PORT.

The Arduino side is working, did someone say MATLAB? Matlab.

Reading serial data into MATLAB is quite simple, once every forum has been sifted through. I will do my best to be as thorough as possible.

links used:
Locating COM PORT PC/MAC/LINUX
fgets fclose fopen
ArduinoMatlab.org/serial MORE USEFUL VIDEO

Connecting Arduino to a Mac for Serial to Matlab

First I will directly past the working code, it is not perfect, but it currently what is needed.

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------Matlab Serial Reading for Angle Encoder----------------------------------
--------------------------------------------------------------------------------------------------------------------------
% Date: 31 May 2016
% Start serial communication between an Arduino and MatLAB.
% If setup is complete the arduino will output a 1, else a 0.
%% SET CONDITIONS for MatLAB to read in ARDUINO SERIAL CHARACTER OUTPUT
s = serial('/dev/tty.usbmodem1411'); % Set com location, this is for a Mac
set(s,'DataBits',8);   % Set to same values on Arduino, can be found at arduino.cc 
set(s,'StopBits',1); 
set(s,'BaudRate',9600); % important, needed, double check for this error
set(s,'Parity','none');

% This can also be entered in in one (1) line as follows:
% s = serial('/dev/tty.usbmodem1411','DataBits',8,'StopBits',1,'BaudRate',9600,'Parity','none');
%% Open the connection to MatLAB
fopen(s); % connects serial object 's' to the arduino
%% Using an infinite loop read (GET) the INFORMATION that is RELEVANT
while (1==1)
    fgets(s) % Use a ';' if you want this info to not be seen this is the 
             % information that it is wanted for the Angel Encoder.
             % This will returned IDENTICAL to that of the Arduino IDE
             % serial output.
   
end
--------------------------------------------------------------------------------------------------------------------------

If you find that to end your code you must close MATLAB down entirely check out this quick video that was made to get around the horror of restarting MATLAB.

Future work.
1. Connect to a PID controller to regulate a motor that will spin something that this encoder measures.
2. Using a planetary gear system create a ratio that is useful to scientists. For example: if 1/10th of a degree is useful to these folks make a gear ratio of 10 to 1, (encoder to Viper Journal Bearing Mount)






Friday, May 27, 2016

Micro Controlled Power Supply 1

Day 5
5/27

 We were tasked with making a micro controlled power supply in order to control the speed of our motor. To do this we need one 22uF capacitor, two 1000uF capacitors, one inductor, one 10k resistor, two 100k resistors, two diodes, and an Opamp. We set up the micro controlled power supply in a circuit but were unable to complete it because some items were not in stock.

The above picture is what the final product will look like.

Thursday, May 26, 2016

Arduino Mega 2560 NOT CAPABLE of Analog Output

Day 4
May 26th, 2016

We found the only Arduino capable, with Simulink, of Analog Output is the Due. With that,
the Leader 718-3D Power Supply was not remotely tunable as hoped. So, it is not completely terrible that our progress was cut short in the beginning.

We are looking into controlling a GPIB with Simulink and possibly the Mega 2560 that we have or a Zed Board.

Controlling Motor Directional and On/Off power with Relay

Day 4
May 26th, 2016

To have further control of the motors we decided to integrate the CCW and CW JZC-11F Relay circuits into a Simulink diagram. We were successfully able to control the relays, with minimal issues, directly from the Arduino Mega 2560. The hard circuit was on a proto-board, where we used the power from that for the external loads.

Simulink Panel: The switches were linked to the respective constant values that would either release a 1 or 0 depending upon the action the user wanted. The outputs went to digital pins that would then turn a relay on or off performing the task it was designed to do.



Physical Hard Wiring of the circuit. The DC Motor is on the bottom between two knobs. We found that it was easiest to Turn the motor off then to switch directions.

Relay Autopsy

Day 4

May 26th 2016

At the end of yesterday's work a relay was broken, today we were intrigued at the insides of the little black box so we crushed the plastic casing to expose the guts of the relay.  Tried to visualize the inside in action, we could see as the solenoid receives power, it produces a magnetic field that attracts a metal button that hangs above it to be pulled down and this action results in the switch being flipped, we even went as far as to unravel a solenoid, the wire must have been 50 feet long which is surprising for how small the solenoid was.

Relay On/Off

Day 4
5/26

Wired the arduino and a JZC-11F relay together and used simulink blocks to control the on/off function of a motor.


The picture above is the circuit only controlling the on/off function. Later, this circuit was integrated with a photo-board to be able to control the direction of the motor with two more relays.

PID Control, Motor Direction Controll

Day 1 and 2
May 23rd and 24th, 2016

Installation of MatLAB 2016a and Simulink 2016 with the Arduino support package (released date: May of 2016) on all three (3) of the students computers.

The Arduino Mega 2560 was connected to the Simulink program and we began work on a temperature sensor system. It used a TMP 36GZ temperature sensor and a simple algorithm to display the temperature in a common scale.
------------------------------------------------------------------------------------------------------------------------

After we became somewhat familiar with Simulink we explored the built in PID controller using the temperature sensor and a fan. The idea was to keep the temperature sensor at a cool temperature.

From what we saw it worked when ice was placed near the sensor, making the error value smaller and smaller as the set temperature was reached, but the fan continued to spin as the error became zero then passed zero, we will be exploring if statements next.
------------------------------------------------------------------------------------------------------------------------

Day 3
May 25th, 2016

Dr. Hassel mentioned that controlling the direction of the motor is something that is necessary, so we began to work on making a system that will change polarization of the motors from an arduino system. Two (2) JZC-11F relays were used to switch which pole of the motor would be connected to ground and voltage source, allowing for forward and reverse of the motors. A schematic will be posted in the future when this system is used on an arduino board and simulink model.


------------------------------------------------------------------------------------------------------------------------




PWM to control relay

Day 3

On Tuesday, May 24, 2016 we attempted to control a relay with pulse width modulation unsuccessfully because the speed of the pulses was to fast for the relay to react.  Below is a picture of the simulink we used to attempt this process.

PID to control motor

Day 2
5/24

A PID was used with this JZC-11F relay to control an external motor. A relay was set with an external load and an external source. The coil was connected to the arduino on one side and ground separate from the arduino.
 

simulated controlled voltage source

Day 3

On Wednesday, 5/25/2016, we used the Leader (Leader 718-3D DC voltage source) along with a 10k potentiometer and a red LED to visualize a controlled voltage source that utilized pulse width modulation.  As shown below,  we used simulink to produce a constant value that could vary between zero and 255, this minimum and maximum value was discovered by starting with a larger window and testing how high we could make the constant while still changing the voltage, the max of 255 comes from the max number of bits the arduino can produce at any given time for pulse width modulation.  The main error with this is that the periods of the pulse width modulation are not phase correct. As a result the voltages that were produced by the arduino board were not linear, it made a strange sine wave.

Controlled Voltage Source

Day 3
5/25

We needed to be able to control the voltage of our circuit. In order to do this, we found a Leader 718-3D DC source pictured below:
 
To learn how to operate this device we consulted an online instruction manual for the Leader 718-3D. The eighth page of chapter four was the most useful in determining the setup for the DC source. 
This scheme was used to turn on and off a fan which represents the motor that will be used, in the final section of the project, to control the angle of the Viper telescope.