Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:2b10383306a2
- Child:
- 1:0b0b7e0b6712
diff -r 000000000000 -r 2b10383306a2 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Nov 10 11:47:25 2011 +0000
@@ -0,0 +1,223 @@
+//
+// 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"
+
+
+AnalogIn sensor1(p15);
+AnalogIn sensor2(p16);
+AnalogOut valueLED1(p18);
+DigitalOut valueLED2(p25);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut clk(p24);
+
+
+
+//******************************************************************************
+// declare set of system objects
+//
+
+//
+//
+// 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;
+ }
+ 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] = last_servo;
+ break;
+//
+// catch any problems
+//
+ default:
+ command->nos_data = 0; // no data to be returned
+ command->result_status = CMD_BAD_SERVO_VALUE;
+ break;
+ }
+ return OK;
+}
+
+//************************************************************************
+//************************************************************************
+//
+int main() {
+ valueLED1=0;
+ valueLED2=0;
+ clk=0;
+ init_sys();
+ FOREVER {
+ 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);
+
+
+ //program for 2 sensors
+
+
+ clk = !clk;
+
+ wait(0.01);
+
+ sensor1.read();
+ sensor2.read();
+
+ if(sensor1 > 0.5) {
+ led1 = 1;
+ valueLED1 = 1;
+ }
+ else if(sensor1 < 0.5){
+ led1 = 0;
+ valueLED1 = 0;
+ }
+
+
+ if(sensor2 > 0.5) {
+ led2 = 1;
+ valueLED2 = 1;
+ }
+ else if(sensor2 < 0.5){
+ led2 = 0;
+ valueLED2 = 0;
+ }
+ }
+
+}