Nurbol Nurdaulet / Mbed 2 deprecated 2sensors_2servo_17_11_11

Files at this revision

API Documentation at this revision

Comitter:
Nurbol
Date:
Thu Nov 17 17:03:22 2011 +0000
Commit message:

Changed in this revision

MCP23017.lib Show annotated file Show diff for this revision Revisions of this file
WattBob_TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
cmd_io.cpp Show annotated file Show diff for this revision Revisions of this file
cmd_io.lib Show annotated file Show diff for this revision Revisions of this file
globals.cpp Show annotated file Show diff for this revision Revisions of this file
globals.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23017.lib	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Nurbol/code/MCP23017/#69c047b34ca6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WattBob_TextLCD.lib	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Nurbol/code/WattBob_TextLCD/#fd972c79270d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_io.cpp	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,161 @@
+//
+// cmd_io.cpp : library of routines to communicate with virtual COM port
+// ==========
+//
+#include    "mbed.h"
+#include    "cmd_io.h"
+#include    "globals.h"
+
+//************************************************************************
+//
+// get_cmd - read a command string from the serial port
+// =======
+// 
+// Method
+//      1. Read string of data until a Newline character
+//            string is passed as the parameter
+//      2. return a sting through the pointer and a count of the number of characters
+//
+// Notes
+//
+uint32_t get_cmd(CMD_STRUCT *command)
+{
+char   ch;
+uint8_t  i;
+//
+// read string and use newline as terminator
+//
+    for (i= 0  ; i < CMD_STR_BUFFER_SZ ; i++) {
+        ch = pc.getc();
+        if (ch == '\r') {
+            continue;
+        }
+        command->cmd_str[i] = ch;
+        if (ch == '\n') {
+            command->cmd_str[i] = '\0';
+            break;
+        }
+    }
+    command->char_cnt = i;    // record character count
+    command->result_status = OK;
+    return 0;
+}
+
+//************************************************************************
+//
+// parse_cmd - split command into its component parts
+// =========
+// 
+// Method
+//      1. first character is the command code
+//      2. subsequent character define a set of parameters
+//
+//        String is located in the CMD_STRUCT and the resulting
+//        data is returned to the same structure.
+//
+// Notes
+//
+uint32_t parse_cmd(CMD_STRUCT *command)
+{
+uint8_t  param_cnt, i, state;
+uint16_t tmp;
+
+    command->cmd_code = command->cmd_str[0];
+    param_cnt = 0;
+    tmp = 0;
+    state = 0;
+    for (i=1 ; i < CMD_STR_BUFFER_SZ ; i++) {    // step through characters
+        if (command->cmd_str[i] == '\0') {
+            if (state == 1) {     // count paramter that is terminated by NEWLINE
+                command->param[param_cnt] = tmp;
+                param_cnt++;
+            }
+            command->nos_params = param_cnt;
+            return OK;
+        }
+        if ((command->cmd_str[i] == ' ') && (state == 0)) {        // skip spaces
+            continue;        
+        }
+        if ((command->cmd_str[i] == ' ') && (state == 1)) {        // skip spaces
+            state = 0;
+            command->param[param_cnt] = tmp;
+            param_cnt++;
+            tmp = 0;
+            continue;        
+        }
+        state = 1;
+        if ((command->cmd_str[i] >= '0') && (command->cmd_str[i] <= '9')) {
+            tmp = (tmp * 10) + (command->cmd_str[i] - '0');
+        } else {
+            command->param[param_cnt] = tmp;
+            return CMD_BAD_CHARACTER;
+        }
+    }
+    return CMD_NO_TERMINATOR;
+}
+
+//************************************************************************
+//
+// reply_to_cmd - return data and status info
+// ============
+// 
+// Method
+//
+// Notes
+//
+void reply_to_cmd(CMD_STRUCT *command)
+{
+    send_status(command->result_status);
+    if ((command->result_status == OK) && (command->nos_data > 0)) {
+        send_data(command);
+    }
+}
+
+//************************************************************************
+//
+// send_status - command status
+// ===========
+//
+// Notes
+//        return status value as a positive interger in a string format
+//        with a terminating newline character.
+//
+void send_status(uint32_t  value)
+{
+    pc.printf("%d\n", value);
+    return;
+}
+
+//************************************************************************
+//
+// send_data - send data appropriate to the command 
+// =========
+// 
+// Method
+//
+// Notes
+//
+void send_data(CMD_STRUCT *command)
+{
+char buffer[80];
+uint8_t i;
+//
+// create data string
+//
+    if (command->nos_data == 1) {
+        sprintf(buffer, "%u\n", (int)command->result_data[0]);
+    }
+    if (command->nos_data == 2) {
+        sprintf(buffer, "%u %u\n", (int)command->result_data[0], (int)command->result_data[1]);
+    }
+//
+// send string
+//
+    for (i = 0 ; i < 40 ; i++) {
+        if (buffer[i] == '\0') {    // do not send NULL character
+            return;
+        }
+        pc.putc(buffer[i]);
+    }
+    return;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_io.lib	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Nurbol/code/cmd_io/#1722aea42640
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globals.cpp	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,2 @@
+#include    "mbed.h"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globals.lib	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Nurbol/code/globals/#ca74c2573e8d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,309 @@
+//
+// COM_test : program to communicate via a COM port to a PC/laptop
+// ========
+//
+// Description
+//      Program to receive ASCII format commands through a virtual COM port and
+//      after executing the command return some data (optional, depending on
+//      the command) and a status value.
+//      The program understands two possible commands :
+//          "s i j"     : move servo 'i (0 to 5) to postion 'j' (0 to 90)
+//          "r"         : read the 'i' parameter of the last "s" command
+//
+// Version : 1.0
+// Author : Jim Herd
+// Date : 5th Oct 2011
+//
+#include "mbed.h"
+#include "MCP23017.h"
+#include "WattBob_TextLCD.h"
+#include "cmd_io.h"
+#include "globals.h"
+
+
+
+Ticker timer1p;
+Ticker timer2p;
+Ticker timercounter1p;
+Ticker timercounter2p;
+
+
+AnalogIn sensor1(p15);
+AnalogIn sensor2(p16);
+AnalogOut valueLED1(p18);
+DigitalOut valueLED2(p25);
+
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+DigitalOut clk(p24);
+
+AnalogIn counter1p(p19);
+AnalogIn counter2p(p20);
+
+
+//******************************************************************************
+// declare functions 
+//
+void sensor1p (void);
+void sensor2p (void);
+void counter1 (void);
+void counter2 (void);
+
+//    
+//
+// 2. five servo outputs
+//
+
+PwmOut servo_0(p26);
+//PwmOut servo_1(p25);
+//PwmOut servo_2(p24);
+//PwmOut servo_3(p23);
+PwmOut servo_4(p22);
+//PwmOut servo_5(p21);
+
+//
+// 3. objects necessary to use the 2*16 character MBED display
+//
+MCP23017            *par_port;
+WattBob_TextLCD     *lcd;
+//
+// 4. Virtual COM port over USB link to laptop/PC
+//
+Serial      pc(USBTX, USBRX);
+
+//******************************************************************************
+// Defined GLOBAL variables and structures
+//
+CMD_STRUCT         ext_cmd;     // structure to hold command data
+STAT_STRUCT        ext_stat;    // structure to hold status reply
+
+uint32_t   last_servo;          // store for last servo number
+
+//************************************************************************
+//************************************************************************
+// init_sys : initialise the system
+// ========
+//
+// 1. Configure 2*16 character display
+// 2. Print "COM test" string
+// 3. initialise relevant global variables
+// 4. set COM port baud rate to 19200 bits per second
+//
+void init_sys(void) {
+
+    par_port = new MCP23017(p9, p10, 0x40);
+    lcd = new WattBob_TextLCD(par_port);
+
+    par_port->write_bit(1,BL_BIT);   // turn LCD backlight ON
+    lcd->cls();
+    lcd->locate(0,0);
+    lcd->printf("COM ");
+    
+    servo_0.period(0.020);    // servo requires a 20ms period, common for all 5 servo objects
+    last_servo = SERVO_UNKNOWN; 
+    pc.baud(19200);
+
+    return;
+}           // end init_sys
+
+//************************************************************************
+// process_cmd : decode and execute command
+// ===========
+uint32_t process_cmd(CMD_STRUCT *command)
+{
+int32_t pulse_width;
+
+    switch (command->cmd_code) {
+//
+// move a servo
+//
+        case SERVO_CMD :
+            command->nos_data = 0;                   // no data to be returned
+    //
+    // check that parameters are OK
+    //
+            if (command->nos_params != 2) {          // check for 2 parameters
+                command->result_status = CMD_BAD_NUMBER_OF_PARAMETERS;
+                break;
+            }
+            if (command->param[0] > MAX_SERVO_NUMBER ) {       // check servo number
+                command->result_status = CMD_BAD_SERVO_NUMBER;
+                break;
+            }
+            if ((command->param[1] < MIN_SERVO_ANGLE) ||
+                (command->param[1] > MAX_SERVO_ANGLE) ) { 
+                command->result_status = CMD_BAD_SERVO_VALUE;
+                break;
+            }
+            if ((command->param[0] == 4) && (command->param[1] == 0)) {
+                 pulse_width = 0;  // convert angle to pulse width
+                 }
+            else{ 
+                pulse_width = 1000 + (command->param[1] * 1000) / MAX_SERVO_ANGLE;  // convert angle to pulse width   
+            }
+             
+            
+            
+    //
+    // implement servo move to all 5 servos
+    //
+            switch (command->param[0]) {
+               case 0 : servo_0.pulsewidth_us(pulse_width); break;
+           //     case 1 : servo_1.pulsewidth_us(pulse_width); break;
+          //      case 2 : servo_2.pulsewidth_us(pulse_width); break;
+           //     case 3 : servo_3.pulsewidth_us(pulse_width); break;
+               case 4 : servo_4.pulsewidth_us(pulse_width); break;
+           //     case 5 : servo_5.pulsewidth_us(pulse_width); break;
+                
+            }
+            last_servo = command->param[0];
+            break;
+//
+// return last servo number
+//
+        case READ_CMD :
+            command->nos_data = 1;                   // no data to be returned
+            command->result_data[0] = valueLED1;
+            command->result_data[1] = valueLED2;
+            break;
+//
+// catch any problems
+//            
+        default:
+            command->nos_data = 0;                   // no data to be returned
+            command->result_status = CMD_BAD_SERVO_VALUE;
+            break;
+    }
+    return  OK;
+}
+
+//************************************************************************
+
+//function 
+void sensor1p (void){
+
+
+    clk = !clk;
+    wait(0.01);
+    sensor1.read();
+        if(sensor1 > 0.5) {
+                led1 = 1;
+                valueLED1 = 1;
+        }
+        else if(sensor1 < 0.5){
+               led1 = 0;
+                valueLED1 = 0;
+        }
+}
+
+
+void sensor2p (){
+
+    sensor2.read();
+        if(sensor2 > 0.5) {
+                led2 = 1;
+                valueLED2 = 1;
+        }
+        else if(sensor2 < 0.5){
+               led2 = 0;
+                valueLED2 = 0;
+        }
+}
+
+void counter1 (){
+    if(counter1p > 0.5){
+            led3 = 1;
+         }
+         else{
+            led3 = 0;
+         }
+}
+
+void counter2(){
+    if(counter2p > 0.5){
+            led4 = 1;
+            process_cmd(&ext_cmd);
+         }
+         else{
+            led4 = 0;
+         }
+}
+
+
+
+
+//************************************************************************
+//
+int main() {
+
+    valueLED1=0;
+    valueLED2=0;
+    clk=0;
+    init_sys();
+    
+    
+    
+    
+    
+    FOREVER {
+    
+    timer2p.attach(&sensor2p, 0.1);
+    timer1p.attach(&sensor1p, 0.1);
+    
+    
+     clk = !clk;
+     wait(0.1);
+     
+    timercounter2p.attach(&counter2, 0.1);
+    timercounter1p.attach(&counter1, 0.1);
+     
+     
+   
+
+    
+ 
+    
+        get_cmd(&ext_cmd);
+    //
+    // Check status of read command activity and return an error status if there was a problem
+    // If there is a problem, then return status code only and wait for next command.
+    //
+        if (ext_cmd.result_status != OK){
+            send_status(ext_cmd.result_status);
+            continue;
+        }
+    //
+    // Parse command and return an error staus if there is a problem
+    // If there is a problem, then return status code only and wait for next command.
+    //
+        parse_cmd(&ext_cmd);
+        lcd->locate(1,0); lcd->printf(ext_cmd.cmd_str);
+        if ((ext_cmd.result_status != OK) && (ext_cmd.cmd_code != TEXT_CMD)){
+            lcd->locate(1,0); lcd->printf("parse : error");
+            send_status(ext_cmd.result_status);
+            continue;
+        }
+    //
+    // Execute command and return an error staus if there is a problem
+    //
+        process_cmd(&ext_cmd);
+        reply_to_cmd(&ext_cmd);
+        
+       
+    
+    }
+         
+}
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 17 17:03:22 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912