..

Dependencies:   Servo mbed

Committer:
brianconnett
Date:
Mon Aug 18 18:05:21 2014 +0000
Revision:
0:a294a5353234
ES305 Lab2 Exercise 5; Control of Servo with Potentiometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brianconnett 0:a294a5353234 1 //****************************************
brianconnett 0:a294a5353234 2 // ES305 Linear Control Systems
brianconnett 0:a294a5353234 3 // Lab 2 - Introduction to mbed microcontroller
brianconnett 0:a294a5353234 4 // Exercise 5 - Controlling servo position with PWM and variable potentiometer
brianconnett 0:a294a5353234 5 //
brianconnett 0:a294a5353234 6 // Brian Connett, LCDR, USN
brianconnett 0:a294a5353234 7 // exercise and code derived from mbed.org
brianconnett 0:a294a5353234 8 // exercise and code derived from Curtis Shorr, Capt, USAF
brianconnett 0:a294a5353234 9 //****************************************
brianconnett 0:a294a5353234 10
brianconnett 0:a294a5353234 11 /*********************************************************************************/
brianconnett 0:a294a5353234 12 //SCY202 LAB 2 - Introduction to Cyber Attack
brianconnett 0:a294a5353234 13 //Program Name: Motor Control
brianconnett 0:a294a5353234 14 //Program Description: This program uses the mbed application board to control
brianconnett 0:a294a5353234 15 // an RC motor, DC motor and peltier device. With user interface via 2
brianconnett 0:a294a5353234 16 // potentiometers and lcd screen.
brianconnett 0:a294a5353234 17 // Created by Capt Curtis Shorr, USAF, Instructor USNA
brianconnett 0:a294a5353234 18 // Libraries and Code derived from mbed.org online community
brianconnett 0:a294a5353234 19 /*********************************************************************************/
brianconnett 0:a294a5353234 20
brianconnett 0:a294a5353234 21
brianconnett 0:a294a5353234 22 #include "mbed.h" //include mbed library
brianconnett 0:a294a5353234 23 #include "Servo.h" //include Servo library
brianconnett 0:a294a5353234 24
brianconnett 0:a294a5353234 25 AnalogIn pot1(p15); //declare pot1
brianconnett 0:a294a5353234 26 Serial pc(USBTX,USBRX); //declare serial comms over USB;
brianconnett 0:a294a5353234 27 Servo rc_motor(p21); //delcare Servo Motor
brianconnett 0:a294a5353234 28
brianconnett 0:a294a5353234 29 int main()
brianconnett 0:a294a5353234 30 {
brianconnett 0:a294a5353234 31
brianconnett 0:a294a5353234 32
brianconnett 0:a294a5353234 33 rc_motor.calibrate(0.0009, 90.0); /********************** Servo Motor Calibration **************************/
brianconnett 0:a294a5353234 34
brianconnett 0:a294a5353234 35
brianconnett 0:a294a5353234 36 float motpos; //declare servo position variable
brianconnett 0:a294a5353234 37 float rc_control;
brianconnett 0:a294a5353234 38 while(1) {
brianconnett 0:a294a5353234 39
brianconnett 0:a294a5353234 40 /********************* Servo Motor Control ***********************/
brianconnett 0:a294a5353234 41 rc_control = pot1.read();//reads pot1 input
brianconnett 0:a294a5353234 42 rc_motor = rc_control; //send actuation command to servo
brianconnett 0:a294a5353234 43
brianconnett 0:a294a5353234 44 motpos = pot1.read(); //read pot2 input & set as servo position
brianconnett 0:a294a5353234 45 motpos *= 1800; //180 degrees with three sig figs
brianconnett 0:a294a5353234 46 motpos = (float)((int)motpos);
brianconnett 0:a294a5353234 47 motpos /= 10;
brianconnett 0:a294a5353234 48 pc.printf("RC Position:%9.6f\r",rc_control*180);
brianconnett 0:a294a5353234 49 }
brianconnett 0:a294a5353234 50 }