Simply creates a servo object from a motor object, to allow the control of the angle. uses the solar panel to look for the brightest spot, then stops.

Dependencies:   mbed

Fork of Lab6_Basic by ECE 111 At Oregon State University

Committer:
dogcatfee
Date:
Tue Nov 14 13:07:24 2017 -0800
Revision:
8:0d8f931d6f8c
Parent:
6:65dfd3886629
Remove SLCD.lib, hg remove SLCD.lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dogcatfee 6:65dfd3886629 1 #include "mbed.h"
dogcatfee 6:65dfd3886629 2 #include "Servo.h"
dogcatfee 6:65dfd3886629 3 // gloabl varevlbe used only inseide the class
ziadeldebri 3:b787aa49b900 4 int value_current;
ziadeldebri 3:b787aa49b900 5 int value_target;
ziadeldebri 3:b787aa49b900 6 float value_target_raw;
ziadeldebri 3:b787aa49b900 7 Ticker tick;
ziadeldebri 3:b787aa49b900 8 SLCD slcd;
ziadeldebri 3:b787aa49b900 9 int n;
ziadeldebri 3:b787aa49b900 10 char buffer[50];
dogcatfee 6:65dfd3886629 11 unsigned int counter;
ziadeldebri 3:b787aa49b900 12
ziadeldebri 3:b787aa49b900 13 Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed):
dogcatfee 6:65dfd3886629 14 _feedback(analog_input), _motor(Positive,Negative,Speed)
dogcatfee 6:65dfd3886629 15 {
dogcatfee 6:65dfd3886629 16
ziadeldebri 3:b787aa49b900 17 }
dogcatfee 6:65dfd3886629 18 /*
dogcatfee 6:65dfd3886629 19 Degree should be a value from 0 to 180.
dogcatfee 6:65dfd3886629 20 Degree is really from 0 to 100(percent).
dogcatfee 6:65dfd3886629 21 */
dogcatfee 6:65dfd3886629 22 void Servo::set(int degree)
dogcatfee 6:65dfd3886629 23 {
dogcatfee 6:65dfd3886629 24 /*
dogcatfee 6:65dfd3886629 25 Sets value_target to the degree value that user wants servo to go to.
dogcatfee 6:65dfd3886629 26 */
dogcatfee 6:65dfd3886629 27 value_target = degree;
dogcatfee 6:65dfd3886629 28 /*
dogcatfee 6:65dfd3886629 29 Counter to store number of tries for motor to be in position.
dogcatfee 6:65dfd3886629 30 */
dogcatfee 6:65dfd3886629 31 counter = 0;
dogcatfee 6:65dfd3886629 32 /*
dogcatfee 6:65dfd3886629 33 Add the servo's move function to the ticker, runs every 1ms.
dogcatfee 6:65dfd3886629 34 */
dogcatfee 6:65dfd3886629 35 tick.attach(this, &Servo::move, 0.01); // set the ticker to call move every 0.001 sec
dogcatfee 6:65dfd3886629 36 }
ziadeldebri 3:b787aa49b900 37
dogcatfee 6:65dfd3886629 38 void Servo::move()
dogcatfee 6:65dfd3886629 39 {
dogcatfee 6:65dfd3886629 40 /*
dogcatfee 6:65dfd3886629 41 value_target is destination
dogcatfee 6:65dfd3886629 42 value_current is read from analog
dogcatfee 6:65dfd3886629 43 slcd for the LCD display
dogcatfee 6:65dfd3886629 44 */
ziadeldebri 3:b787aa49b900 45
dogcatfee 6:65dfd3886629 46 /* Read the potentiometer value. */
dogcatfee 6:65dfd3886629 47 value_current = _feedback*100;
dogcatfee 6:65dfd3886629 48 /*
dogcatfee 6:65dfd3886629 49 When target value is higher relative to
dogcatfee 6:65dfd3886629 50 the value read from the potentiometer
dogcatfee 6:65dfd3886629 51 rotate to the left (Couter-Clockwise).
dogcatfee 6:65dfd3886629 52 */
dogcatfee 6:65dfd3886629 53 if(value_target > value_current)
dogcatfee 6:65dfd3886629 54 {
dogcatfee 6:65dfd3886629 55 _motor.Direction(LEFT);
dogcatfee 6:65dfd3886629 56 counter ++;
dogcatfee 6:65dfd3886629 57 }
dogcatfee 6:65dfd3886629 58 /*
dogcatfee 6:65dfd3886629 59 When target value is lower relative to
dogcatfee 6:65dfd3886629 60 the value read from the potentiometer
dogcatfee 6:65dfd3886629 61 rotate to the right. (Clockwise).
dogcatfee 6:65dfd3886629 62 */
dogcatfee 6:65dfd3886629 63 else if(value_target < value_current)
dogcatfee 6:65dfd3886629 64 {
dogcatfee 6:65dfd3886629 65 _motor.Direction(RIGHT);
dogcatfee 6:65dfd3886629 66 counter ++;
dogcatfee 6:65dfd3886629 67 }
dogcatfee 6:65dfd3886629 68 /*
dogcatfee 6:65dfd3886629 69 When the motor has been set 2000 times
dogcatfee 6:65dfd3886629 70 change the target value to the current
dogcatfee 6:65dfd3886629 71 value of the analog reading, set the
dogcatfee 6:65dfd3886629 72 counter to 0.
ziadeldebri 3:b787aa49b900 73
dogcatfee 6:65dfd3886629 74 Used to prevent rocking between values
dogcatfee 6:65dfd3886629 75 when the MBED cannot get the value from
dogcatfee 6:65dfd3886629 76 the potentiometer.
dogcatfee 6:65dfd3886629 77 */
dogcatfee 6:65dfd3886629 78 else if(counter > 2000)
dogcatfee 6:65dfd3886629 79 {
dogcatfee 6:65dfd3886629 80 value_target = value_current;
dogcatfee 6:65dfd3886629 81 counter = 0;
dogcatfee 6:65dfd3886629 82 }
dogcatfee 6:65dfd3886629 83 /*
dogcatfee 6:65dfd3886629 84 If no if statements are true, stop
dogcatfee 6:65dfd3886629 85 the motor and detatch the tick process.
dogcatfee 6:65dfd3886629 86 */
dogcatfee 6:65dfd3886629 87 else
dogcatfee 6:65dfd3886629 88 {
dogcatfee 6:65dfd3886629 89 tick.detach();
dogcatfee 6:65dfd3886629 90 _motor.Stop();
ziadeldebri 4:b3a93554fedf 91 }
dogcatfee 6:65dfd3886629 92 /*
dogcatfee 6:65dfd3886629 93 Sprintf to copy the current value into,
dogcatfee 6:65dfd3886629 94 the buffer( n stores status of sprintf()).
dogcatfee 6:65dfd3886629 95 */
dogcatfee 6:65dfd3886629 96 n = sprintf (buffer, "%d", value_current);
dogcatfee 6:65dfd3886629 97 slcd.clear(); /* Clear the SLCD */
dogcatfee 6:65dfd3886629 98 slcd.Home(); /* Set the decimal point on SLCD */
dogcatfee 6:65dfd3886629 99 slcd.printf(buffer); /* Write the buffer to the SLCD */
dogcatfee 6:65dfd3886629 100 }
dogcatfee 6:65dfd3886629 101 /*
dogcatfee 6:65dfd3886629 102 Check function to write to SLCD,
dogcatfee 6:65dfd3886629 103 not used.
dogcatfee 6:65dfd3886629 104 */
dogcatfee 6:65dfd3886629 105 void Servo::check()
dogcatfee 6:65dfd3886629 106 {
dogcatfee 6:65dfd3886629 107 value_current = _feedback*100;
dogcatfee 6:65dfd3886629 108 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 109 slcd.clear();
dogcatfee 6:65dfd3886629 110 slcd.Home();
ziadeldebri 3:b787aa49b900 111 slcd.printf(buffer);
dogcatfee 6:65dfd3886629 112 }
dogcatfee 6:65dfd3886629 113 /*
dogcatfee 6:65dfd3886629 114 Returns current position of the analog dial.
dogcatfee 6:65dfd3886629 115 0 to 100.
dogcatfee 6:65dfd3886629 116 */
dogcatfee 6:65dfd3886629 117 unsigned int Servo::get()
dogcatfee 6:65dfd3886629 118 {
dogcatfee 6:65dfd3886629 119 value_current = _feedback*100;
dogcatfee 6:65dfd3886629 120 return value_current;
dogcatfee 6:65dfd3886629 121 }