Difference between revisions of "VelTek shield motor step"

From Training Technology Lab Wiki
Jump to navigation Jump to search
Line 51: Line 51:
  
 
== Creating velocity feedback ==
 
== Creating velocity feedback ==
Every line of code inside the Arduino function '''void loop()''' (bottom of Arduino sketch file veltek-shield-motor-ab-encoder.ino) will be executed repeatedly as fast as possible, but we don't have any control over exactly how fast. This is bad for control systems where we want to know the sample rate in Hz. An easy fix is to create a function that can measure if a fixed amount of time has passed. This is exactly what checkTime() does. It returns true if 20 ms (or more) has passed and false otherwise. You can find this '''if(checkTime())''' statement in the void loop() function:  
+
Every line of code inside the Arduino function '''void loop()''' (bottom of Arduino sketch file veltek-shield-motor-ab-encoder.ino) will be executed repeatedly as fast as possible, but we don't have any control over exactly how fast. This is bad for control systems where we want to know the sample rate in Hz. An easy fix is to create a function that can measure if a fixed amount of time has passed. This is exactly what '''checkTime()''' does. It returns true if 20 ms (or more) has passed and false otherwise. You can find this '''if(checkTime())''' statement in the void loop() function:  
  
 
  if(checkTime())
 
  if(checkTime())

Revision as of 10:08, 13 May 2019

Required hardware components

Arduino Uno (or similar)
VelTek shield (r2, blue)
DC motor w. encoder
12V AC/DC adaptor
USB A to B cable
Motor cable
Small flat-head screw driver
Recommended: cheap USB hub

Required software

  • Serial terminal program with logging capabilities. We recommend Putty which is available on Windows/Linux/Mac.
  • Math program capable of importing numbers from a text file. Matlab or Octave (free) is recommended, but Microsoft Excel can also be used.
  • Arduino sketch (C/C++ program for the Arduino) File:Veltek-shield-motor-ab-encoder-1.0-beta1.zip that starts the DC motor and count pulses from the encoder on the motor.
  • Arduino IDE to compile and upload the sketch to your Arduino. This is available on Windows/Linux/Mac

Setup

motor encoder cable mounted correctly
File:IMG arduino motor shield connected.JPG
motor connected to VelTek shield
  1. Mount VelTek shield on top of Arduino. Take care that all pins mate correctly to their sockets before pressing together.
  2. Connect motor cable to DC motor. The little white plug can only go in one way. When inserted correctly, the orange wire is towards the edge of the circuitboard. (See picture)
  3. Use screwdriver to connect the red wire to "OUTB" and black wire to "OUTA" on the green terminal on VelTek shield. The red and black wire is for motor voltage that drives the DC motor.
  4. Connect blue wire to Arduino GND, and yellow wire to Arduino 5V. These wires power the encoder circuit on the back of the motor.
  5. Connect violet wire to Arduino pin 3 and orange wire to pin 2. These wires carry the output signals from the encoder on the motor.
  6. Plug the AC/DC adaptor into a 230V wall socket and then insert 12V plug from adaptor into the Arduino.
  7. Optional but strongly reccomended: Connect Arduino board to USB hub.
  8. Connect Arduino or USB hub with Arduino to your computer.


Note: Make sure that you connect the 12V adaptor to your Arduino board before connecting it to your computer via USB.

Usage

Getting started

When the Arduino sketch has been compiled and uploaded, you can check that it is running by opening the serial monitor in the Arduino IDE (icon in top left of the window). When window opens, make sure that you have chosen "115200 baud" as communication speed in drop down menu. You should see two columns of nubers on the screen.

To test that motor and encoder works, try to turn the motor axel by hand. You should see the number in the first column increase og decrease. The number is motor position in milli radians (6283 milli rad / turn)

Click on the text box in top of the serial monitor window, type a single digit from 1-9 and press enter. The motor should start turning. 1 is the lowest speed (voltage), 9 is max voltage (12 V = 12000 mV). Type 0 and enter to stop the motor (0 V)

Creating velocity feedback

Every line of code inside the Arduino function void loop() (bottom of Arduino sketch file veltek-shield-motor-ab-encoder.ino) will be executed repeatedly as fast as possible, but we don't have any control over exactly how fast. This is bad for control systems where we want to know the sample rate in Hz. An easy fix is to create a function that can measure if a fixed amount of time has passed. This is exactly what checkTime() does. It returns true if 20 ms (or more) has passed and false otherwise. You can find this if(checkTime()) statement in the void loop() function:

if(checkTime())
{
   Serial.print(motorpos);
   Serial.print(" ");
   Serial.println(motorcmd);
}

This is responsible for printing out the two columns of numbers (position in milli radians and motor voltage) at a rate of 50Hz (20ms period). If you want to create a feedback system, you can add code here to calculate an error, multiply it with a gain, and feed it to motor.setMotorVoltageMilliVolt().

Hardware description

Datasheet for VelTek shield: File:Arduino Shield Veltek02 19032019.pdf

Software description

Todo