Carbon Fibre / Mbed 2 deprecated Motor_test_harness

Dependencies:   Classic_PID iC_MU mbed-rtos mbed

Committer:
ms523
Date:
Thu Apr 02 07:35:39 2015 +0000
Revision:
0:7ce0bc67f60f
Working as demo'd in the end of March sprint review.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ms523 0:7ce0bc67f60f 1 #include "mbed.h"
ms523 0:7ce0bc67f60f 2 #include "rtos.h"
ms523 0:7ce0bc67f60f 3
ms523 0:7ce0bc67f60f 4 #define AUTO 1
ms523 0:7ce0bc67f60f 5 #define MANUAL 0
ms523 0:7ce0bc67f60f 6
ms523 0:7ce0bc67f60f 7 extern Serial Camera;
ms523 0:7ce0bc67f60f 8 extern bool Mode;
ms523 0:7ce0bc67f60f 9
ms523 0:7ce0bc67f60f 10 void UpdateCamera (float ZoomDemand, float FocusDemand)
ms523 0:7ce0bc67f60f 11 {
ms523 0:7ce0bc67f60f 12 static signed int LastZoom = 0;
ms523 0:7ce0bc67f60f 13 static signed int LastFocus = 0;
ms523 0:7ce0bc67f60f 14
ms523 0:7ce0bc67f60f 15 // The ZoomDemand value passed to this function is a raw read of the AnalogIn object
ms523 0:7ce0bc67f60f 16 ZoomDemand -= 0.5; // Shift the raw value to the centre-tap
ms523 0:7ce0bc67f60f 17 ZoomDemand *= -14; // Scale to get in the -7 to +7 range
ms523 0:7ce0bc67f60f 18 int Zoom = (int)ZoomDemand; // Cast to an int
ms523 0:7ce0bc67f60f 19
ms523 0:7ce0bc67f60f 20 // Do the same thing for Focus
ms523 0:7ce0bc67f60f 21 FocusDemand *= 0x7000; // Scale to the range of 0 to 28672
ms523 0:7ce0bc67f60f 22 FocusDemand += 0x0800; // Add a shift of 2048 to get to 2048 to 30720
ms523 0:7ce0bc67f60f 23 int Focus = (int)FocusDemand & 0xFFC0; // Knock off the last 6 bits of noise!
ms523 0:7ce0bc67f60f 24
ms523 0:7ce0bc67f60f 25 // See if we need to move the Camera Zoom Position
ms523 0:7ce0bc67f60f 26 if(Zoom != LastZoom) {
ms523 0:7ce0bc67f60f 27
ms523 0:7ce0bc67f60f 28 // First calculate the new Zoom Setting
ms523 0:7ce0bc67f60f 29 if (Zoom > 0) {
ms523 0:7ce0bc67f60f 30 // Send the tele command
ms523 0:7ce0bc67f60f 31 Zoom += 0x20;
ms523 0:7ce0bc67f60f 32 } else if(Zoom < 0) {
ms523 0:7ce0bc67f60f 33 // Send the wide command
ms523 0:7ce0bc67f60f 34 Zoom *= -1;
ms523 0:7ce0bc67f60f 35 Zoom += 0x30;
ms523 0:7ce0bc67f60f 36 }
ms523 0:7ce0bc67f60f 37 LastZoom = Zoom; // Update the last know Focus Demand for next time
ms523 0:7ce0bc67f60f 38
ms523 0:7ce0bc67f60f 39 // Send first 4 characters (always the same)
ms523 0:7ce0bc67f60f 40 Camera.putc(0x81);
ms523 0:7ce0bc67f60f 41 Camera.putc(0x01);
ms523 0:7ce0bc67f60f 42 Camera.putc(0x04);
ms523 0:7ce0bc67f60f 43 Camera.putc(0x07);
ms523 0:7ce0bc67f60f 44 Camera.putc(Zoom);
ms523 0:7ce0bc67f60f 45 Camera.putc(0xFF);
ms523 0:7ce0bc67f60f 46 }
ms523 0:7ce0bc67f60f 47
ms523 0:7ce0bc67f60f 48 // Now see if we need to move the Camera Focus Position
ms523 0:7ce0bc67f60f 49 if(Mode == MANUAL && Focus != LastFocus) {
ms523 0:7ce0bc67f60f 50 Camera.putc(0x81); // Camera
ms523 0:7ce0bc67f60f 51 Camera.putc(0x01);
ms523 0:7ce0bc67f60f 52 Camera.putc(0x04);
ms523 0:7ce0bc67f60f 53 Camera.putc(0x48); // Direct focus cmd
ms523 0:7ce0bc67f60f 54 Camera.putc((Focus & 0xF000)>>12); // p variable
ms523 0:7ce0bc67f60f 55 Camera.putc((Focus & 0x0F00)>>8); // q variable
ms523 0:7ce0bc67f60f 56 Camera.putc((Focus & 0x00F0)>>4); // r variable
ms523 0:7ce0bc67f60f 57 Camera.putc(Focus & 0x000F); // s variable
ms523 0:7ce0bc67f60f 58 Camera.putc(0xFF);
ms523 0:7ce0bc67f60f 59
ms523 0:7ce0bc67f60f 60 LastFocus = Focus; // Update the last know Focus Demand for next time
ms523 0:7ce0bc67f60f 61 }
ms523 0:7ce0bc67f60f 62
ms523 0:7ce0bc67f60f 63 // Return the current Zoom position
ms523 0:7ce0bc67f60f 64 Camera.putc(0x81); // Address byte from host to camera - 0x81
ms523 0:7ce0bc67f60f 65 Camera.putc(0x09); // Zoom position inquiry - 8x 09 04 47 ff
ms523 0:7ce0bc67f60f 66 Camera.putc(0x04);
ms523 0:7ce0bc67f60f 67 Camera.putc(0x47);
ms523 0:7ce0bc67f60f 68 Camera.putc(0xFF); // Terminator byte - all 1s
ms523 0:7ce0bc67f60f 69
ms523 0:7ce0bc67f60f 70 }