
Servo program to run two servos corresponding to face detection software with face proximity and position on an x-axis. (Note that Bluetooth is not enabled yet).
main.cpp@1:41e66334a2bb, 2021-03-04 (annotated)
- Committer:
- andrewh2104
- Date:
- Thu Mar 04 18:55:27 2021 +0000
- Revision:
- 1:41e66334a2bb
- Parent:
- 0:54315b1ea3ff
v1.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewh2104 | 1:41e66334a2bb | 1 | //------------Face Detection--------------// |
andrewh2104 | 1:41e66334a2bb | 2 | //Scale and X-axis to corresponding Servos// |
andrewh2104 | 1:41e66334a2bb | 3 | //--------Incl. Bluetooth Serial----------// |
andrewh2104 | 1:41e66334a2bb | 4 | //--------Andrew Holden - 04/3/21---------// |
andrewh2104 | 1:41e66334a2bb | 5 | //----------For all resolutions-----------// |
andrewh2104 | 0:54315b1ea3ff | 6 | |
andrewh2104 | 0:54315b1ea3ff | 7 | #include "mbed.h" |
andrewh2104 | 0:54315b1ea3ff | 8 | #include "stdio.h" |
andrewh2104 | 0:54315b1ea3ff | 9 | #include "stdlib.h" |
andrewh2104 | 0:54315b1ea3ff | 10 | #include "iostream" |
andrewh2104 | 1:41e66334a2bb | 11 | #include "Servo.h" |
andrewh2104 | 0:54315b1ea3ff | 12 | |
andrewh2104 | 0:54315b1ea3ff | 13 | using namespace std; |
andrewh2104 | 0:54315b1ea3ff | 14 | |
andrewh2104 | 0:54315b1ea3ff | 15 | Serial pc(USBTX, USBRX); // TX, RX |
andrewh2104 | 0:54315b1ea3ff | 16 | |
andrewh2104 | 0:54315b1ea3ff | 17 | #if defined(TARGET_LPC1768) |
andrewh2104 | 0:54315b1ea3ff | 18 | Serial blue(p9, p10); // TX, RX |
andrewh2104 | 0:54315b1ea3ff | 19 | #endif |
andrewh2104 | 0:54315b1ea3ff | 20 | |
andrewh2104 | 1:41e66334a2bb | 21 | Servo myservo1(p21); |
andrewh2104 | 1:41e66334a2bb | 22 | Servo myservo2(p22); |
andrewh2104 | 0:54315b1ea3ff | 23 | |
andrewh2104 | 1:41e66334a2bb | 24 | float servo = 0.0; |
andrewh2104 | 1:41e66334a2bb | 25 | float svscale; //face scale between 0-1 |
andrewh2104 | 1:41e66334a2bb | 26 | float svpos; //x-axis pos. between 0-1 |
andrewh2104 | 1:41e66334a2bb | 27 | float round(float svscale) //round to 2 dec. places |
andrewh2104 | 0:54315b1ea3ff | 28 | |
andrewh2104 | 0:54315b1ea3ff | 29 | { |
andrewh2104 | 0:54315b1ea3ff | 30 | // EXAMPLE: |
andrewh2104 | 0:54315b1ea3ff | 31 | // 37.66666 * 100 =3766.66 |
andrewh2104 | 0:54315b1ea3ff | 32 | // 3766.66 + .5 =3767.16 for rounding off value |
andrewh2104 | 0:54315b1ea3ff | 33 | // then type cast to int so value is 3767 |
andrewh2104 | 0:54315b1ea3ff | 34 | // then divided by 100 so the value converted into 37.67 |
andrewh2104 | 1:41e66334a2bb | 35 | float value = (int)(svscale * 100 + .5); |
andrewh2104 | 0:54315b1ea3ff | 36 | return (float)value / 100; |
andrewh2104 | 0:54315b1ea3ff | 37 | } |
andrewh2104 | 0:54315b1ea3ff | 38 | |
andrewh2104 | 0:54315b1ea3ff | 39 | int main() |
andrewh2104 | 0:54315b1ea3ff | 40 | { |
andrewh2104 | 0:54315b1ea3ff | 41 | while(1) |
andrewh2104 | 0:54315b1ea3ff | 42 | { |
andrewh2104 | 0:54315b1ea3ff | 43 | //blue.baud(115200); |
andrewh2104 | 0:54315b1ea3ff | 44 | //pc.baud(115200); |
andrewh2104 | 0:54315b1ea3ff | 45 | |
andrewh2104 | 1:41e66334a2bb | 46 | if(cin >> svscale >> svpos) |
andrewh2104 | 0:54315b1ea3ff | 47 | { |
andrewh2104 | 1:41e66334a2bb | 48 | servo = svscale; |
andrewh2104 | 0:54315b1ea3ff | 49 | |
andrewh2104 | 1:41e66334a2bb | 50 | if(0.00 <= svscale && svscale < 1.00) |
andrewh2104 | 0:54315b1ea3ff | 51 | { |
andrewh2104 | 1:41e66334a2bb | 52 | myservo2 = servo; |
andrewh2104 | 0:54315b1ea3ff | 53 | } |
andrewh2104 | 0:54315b1ea3ff | 54 | else |
andrewh2104 | 0:54315b1ea3ff | 55 | { |
andrewh2104 | 1:41e66334a2bb | 56 | myservo2 = 0.5; //home position |
andrewh2104 | 0:54315b1ea3ff | 57 | } |
andrewh2104 | 0:54315b1ea3ff | 58 | |
andrewh2104 | 1:41e66334a2bb | 59 | servo = svpos; |
andrewh2104 | 1:41e66334a2bb | 60 | |
andrewh2104 | 1:41e66334a2bb | 61 | if(0.00 <= svpos && svpos < 1.00) |
andrewh2104 | 0:54315b1ea3ff | 62 | { |
andrewh2104 | 1:41e66334a2bb | 63 | myservo1 = servo; |
andrewh2104 | 0:54315b1ea3ff | 64 | } |
andrewh2104 | 1:41e66334a2bb | 65 | else |
andrewh2104 | 0:54315b1ea3ff | 66 | { |
andrewh2104 | 1:41e66334a2bb | 67 | myservo1 = 0.5; //home position |
andrewh2104 | 0:54315b1ea3ff | 68 | } |
andrewh2104 | 0:54315b1ea3ff | 69 | } |
andrewh2104 | 0:54315b1ea3ff | 70 | } |
andrewh2104 | 0:54315b1ea3ff | 71 | } |