Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card
Dependencies: BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed
Revision 2:3e7baa3e3fec, committed 2014-09-18
- Comitter:
- mehatfie
- Date:
- Thu Sep 18 23:38:11 2014 +0000
- Parent:
- 1:5731f31f96be
- Child:
- 3:078e9a1e8be3
- Commit message:
- - Revamped whole system and now works without stalling or freezing mid program; - now Object Oriented Based in terms of Devices (inheritance of functions)
Changed in this revision
--- a/Device.cpp Tue Sep 16 18:27:41 2014 +0000
+++ b/Device.cpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,15 +1,15 @@
-/*#include "Device.hpp"
-#include "Motor.hpp"
-#include "VoltageDriver.hpp"
-
-Device* Device::newDevice(int deviceFound){
-
- if(MOTOR)
- return new Motor();
- else if(VOLTAGE_DRIVER)
- return new VoltageDriver();
-
-}
-
- */
-
\ No newline at end of file
+//#include "Device.hpp"
+//#include "Motor.hpp"
+//#include "VoltageDriver.hpp"
+//
+//Device* Device::newDevice(int deviceFound){
+//
+// if(MOTOR)
+// return new Motor();
+// else if(VOLTAGE_DRIVER)
+// return new VoltageDriver();
+//
+//}
+//
+//
+//
\ No newline at end of file
--- a/Device.hpp Tue Sep 16 18:27:41 2014 +0000
+++ b/Device.hpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,73 +1,73 @@
-#ifndef DEVICE_HPP
-#define DEVICE_HPP
-
-#include "Initialization.hpp"
-#include "mbed.h"
-#include "LocalPinNames.h"
-#include "BridgeDriver.h"
-#include "DeviceClasses.h"
-//#include "Motor.hpp"
-//#include "VoltageDriver.hpp"
-
-enum DeviceType{MOTOR, VOLTAGE_DRIVER};
-static const enum DeviceType Device_Map[] = {MOTOR, VOLTAGE_DRIVER};
-const char *DeviceNames[] = {"MOTOR", "VOLTAGE_DRIVER"};
-extern int numDevices = sizeof(DeviceNames)/sizeof(DeviceNames[0]);
-extern int currNumDevices = 0;
-
-/*
-class Device{
-
- public:
- char name[100];
- static Device* newDevice(int);
- int interpret();
-};
-
-extern Device devices[15]; //Initialize array of devices, initially assume 15 devices will be used (will expand as needed)
-
-void addDevice(int deviceFound){
-
- //devices[currNumDevices] = Device::newDevice(deviceFound);
- currNumDevices++;
-}
- */
-
-
-
-
-struct DeviceData {
- DeviceType type;
- char name[MAX_LINE_LENGTH];
- union {
- Voltage_Driver VOLTAGE_DRIVER;
- Motor MOTOR;
- };
-};
-
-extern struct DeviceData devices[15]; //Initialize array of devices, initially assume 15 devices will be used (will expand as needed)
-
-void relayLine(DeviceData device){
- switch(device.type){
- case MOTOR: device.MOTOR.interpret(); break;
- case VOLTAGE_DRIVER: device.VOLTAGE_DRIVER.interpret(); break;
- }
-}
-
-void addDevice(int deviceFound){
-
- //Save the device type
- switch (Device_Map[deviceFound]){
- case MOTOR: devices[currNumDevices].type = MOTOR; break;
- case VOLTAGE_DRIVER: devices[currNumDevices].type = VOLTAGE_DRIVER; break;
- }
-
- strcpy(devices[currNumDevices].name, lineData.word[1]); //Save the Local Name of the Device, the second word in a device adding line is the Local_Name
- relayLine(devices[currNumDevices]); //Give the command to the appropraite class in order to initialize itself based on the line
- currNumDevices++; //Increment the total number of devices created
-}
-
-
-
-#endif
-
+//#ifndef DEVICE_HPP
+//#define DEVICE_HPP
+//
+//#include "Initialization.hpp"
+//#include "mbed.h"
+//#include "LocalPinNames.h"
+//#include "BridgeDriver.h"
+////#include "DeviceClasses.hpp"
+////#include "Motor.hpp"
+////#include "VoltageDriver.hpp"
+//
+//enum DeviceType{MOTOR, VOLTAGE_DRIVER};
+//static const enum DeviceType Device_Map[] = {MOTOR, VOLTAGE_DRIVER};
+//const char *DeviceNames[] = {"MOTOR", "VOLTAGE_DRIVER"};
+//extern int numDevices = sizeof(DeviceNames)/sizeof(DeviceNames[0]);
+//extern int currNumDevices = 0;
+//
+//
+//class Device{
+//
+// public:
+// char name[100];
+// static Device* newDevice(int);
+// int interpret();
+//};
+//
+//extern Device devices[15]; //Initialize array of devices, initially assume 15 devices will be used (will expand as needed)
+//
+//void addDevice(int deviceFound){
+//
+// //devices[currNumDevices] = Device::newDevice(deviceFound);
+// currNumDevices++;
+//}
+//
+//
+//
+//
+///*
+//struct DeviceData {
+// DeviceType type;
+// string name;
+// union {
+// Voltage_Driver VOLTAGE_DRIVER;
+// Motor MOTOR;
+// };
+//};
+//
+//extern struct DeviceData devices[15]; //Initialize array of devices, initially assume 15 devices will be used (will expand as needed)
+//
+//void relayLine(DeviceData device){
+// switch(device.type){
+// case MOTOR: device.MOTOR.interpret(); break;
+// case VOLTAGE_DRIVER: device.VOLTAGE_DRIVER.interpret(); break;
+// }
+//}
+//
+//void addDevice(int deviceFound){
+//
+// //Save the device type
+// switch (Device_Map[deviceFound]){
+// case MOTOR: devices[currNumDevices].type = MOTOR; break;
+// case VOLTAGE_DRIVER: devices[currNumDevices].type = VOLTAGE_DRIVER; break;
+// }
+//
+// devices[currNumDevices].name = lineData.word[1]; //Save the Local Name of the Device, the second word in a device adding line is the Local_Name
+// relayLine(devices[currNumDevices]); //Give the command to the appropraite class in order to initialize itself based on the line
+// currNumDevices++; //Increment the total number of devices created
+//}*/
+//
+//
+//
+//#endif
+//
--- a/DeviceClasses.h Tue Sep 16 18:27:41 2014 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,431 +0,0 @@
-/*#include "mbed.h"
-#include "LocalPinNames.h"
-#include "TextLCD.h"
-#include "BridgeDriver.h"
-*/
-#include "Device.hpp"
-
-
-/**********************************************************************************************************************************/
-/**********************************************************************************************************************************/
-/*************************** <CLASS: Voltage_Driver> ****************************/
-/**********************************************************************************************************************************/
-/**********************************************************************************************************************************/
-
-class Voltage_Driver{
-
- public :
- //uint8_t ch123; //Channel value this device is set to
- int ch;
- int test;
-
- int interpret();
- int forceBrake();
- //int forceFloat();
- float drive();
-
- //Voltage_Driver();
- void setChannel(int channel){
- Voltage_Driver::ch = channel;
- }
-
- int getChannel(){
- return Voltage_Driver::ch;
- }
-};
-
-//Voltage_Driver::Voltage_Driver(void){
-//}
-
-//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
-int Voltage_Driver::interpret(){
- /*
- lcd.cls(); //clear the display
- lcd.setAddress(0,3);
- lcd.printf("Test2 ");
- lcd.setAddress(0,1);
- lcd.printf("wrd1: %s", lineData.word[0]);
- lcd.setAddress(0,2);
- lcd.printf("wrd2: %s", lineData.word[1]);
- wait(3);
-*/
-
-
- char *firstWord = lineData.word[0]; //get the first word in the line
-/*
- lcd.setAddress(0,2);
- lcd.printf("fW: %s", firstWord);
- lcd.setAddress(0,3);
- lcd.printf("ch: %d", ch);
- wait(3);*/
-
- //Initialize the class and device
- if (strncmp(firstWord,"device", 6) == 0){
- //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
-
- test = 10;
-
- if (lineData.numWords != 4){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- char *channel = lineData.word[3]; //Parameter is a number
- int channelValue = atoi(channel);
-
- //Channel Value between 1 to 8
- if (channelValue >= 1 && channelValue <= 8){
-
- setChannel(channelValue);
- //ch = channelValue;
-
- lcd.setAddress(0,2);
- lcd.printf("fW: %s", firstWord);
- //lcd.setAddress(0,3);
- //lcd.printf("ch: %d", getChannel());
- lcd.setAddress(0,3);
- lcd.printf("test: %d", test);
- //lcd.setAddress(0,3);
- //lcd.printf("chString: %s", chString);
- wait(2);
-
- switch (channelValue){
- case 1:
- case 2:
- bridges.enablePwm(BridgeDriver::MOTOR_A, 0);
- break;
- case 3:
- case 4:
- bridges.enablePwm(BridgeDriver::MOTOR_B, 0);
- break;
- case 5:
- case 6:
- bridges.enablePwm(BridgeDriver::MOTOR_C, 0);
- break;
- case 7:
- case 8:
- bridges.enablePwm(BridgeDriver::MOTOR_D, 0);
- break;
- }
- /* lcd.setAddress(0,2);
- lcd.printf("AchannelVal: %d", channelValue);
- lcd.setAddress(0,3);
- lcd.printf("Ach: %d", ch);
- wait(3);*/
-
- }
- else{
- //ERROR Out
- //Unacceptable channel, or conversion function failed when converting string to int
- }
- }
-
- //if the first word is not "device" it means that it's trying to perform a function to a specific Motor
- else{
-
-
-
- //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
- char *localname = lineData.word[0]; //just for the sake of following the variable easily and understanding
- char *func = lineData.word[1];
-
- /******************************************************************************/
- /*** <Func: forceBrake> ***/
- /******************************************************************************/
- if (strncmp(func,"forceBrake", 10) == 0){
-
- if (lineData.numWords != 3){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- bridges.forceBrake(ch);
- }
-
-
- /******************************************************************************/
- /*** <Func: drive> ***/
- /******************************************************************************/
- else if (strncmp(func,"drive", 5) == 0){
- //Order of Line: Local_Name, drive
-
- lcd.setAddress(0,0);
- lcd.printf("lclN: %s ", localname);
- lcd.setAddress(0,2);
- lcd.printf("func: %s ", func);
- //lcd.setAddress(0,3);
- //lcd.printf("3ch: %d ", getChannel());
- lcd.setAddress(0,3);
- lcd.printf("test: %d", test);
- //lcd.setAddress(0,3);
- //lcd.printf("chString: %s ", chString);
- wait(3);
-
- if (lineData.numWords != 2){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
- /*
- int State = 2;
- int channelValue = atoi(chString);
- State = bridges.drive(channelValue, 1); //Turn channel on
- lcd.setAddress(0,2);
- lcd.printf("TEST ch: %d, State: %d", channelValue, State);
- wait(3);*/
-
- }
-
-
- /******************************************************************************/
- /**** <Func: off> ****/
- /******************************************************************************/
- else if (strncmp(func,"drive", 5) == 0){
- //Order of Line: Local_Name, drive, duration
-
- if (lineData.numWords != 2){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- bridges.drive(getChannel(), 0); //Turn channel off
- }
-
- else {
- return 0;
-
- }
- }
- /*
- lcd.setAddress(0,3);
- lcd.printf("ENDch: %d ", getChannel());
- wait(3);*/
-
- return 1;
-}
-
-
-
-/**********************************************************************************************************************************/
-/**********************************************************************************************************************************/
-/******************************** <CLASS: Motor> ********************************/
-/**********************************************************************************************************************************/
-/**********************************************************************************************************************************/
-
-class Motor{
-
- enum BridgeDriver::Motors motor;
-
- public :
- int interpret();
- void enableBrake();
- int forceBrake();
- //int forceFloat();
- float drive();
-
-
- void setMotor(BridgeDriver::Motors selectedMotor){
- motor = selectedMotor;
- }
-
- enum BridgeDriver::Motors getMotor(){
- return motor;
- }
-
-};
-
-//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
-int Motor::interpret(){
- /*
- lcd.cls(); //clear the display
- lcd.setAddress(0,3);
- lcd.printf("Test2 ");
- lcd.setAddress(0,1);
- lcd.printf("wrd1: %s", lineData.word[0]);
- lcd.setAddress(0,2);
- lcd.printf("wrd2: %s", lineData.word[1]);
- wait(3);
-*/
-
- lcd.setAddress(0,2);
- lcd.printf("wrd1: %s", lineData.word[0]);
- lcd.setAddress(0,3);
- lcd.printf("TEST1 ");
- wait(1);
-
- char *firstWord = lineData.word[0]; //splits the str based on a space delimeter
-
- if (strncmp(firstWord,"device", 6) == 0){
- //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
-
- if (lineData.numWords != 4){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- char channel = *lineData.word[3]; //Parameter is a single character, so dereference the point to the word
-
- switch (channel){
- case 'A':
- case 'a':
- setMotor(BridgeDriver::MOTOR_A);
- break;
- case 'B':
- case 'b':
- setMotor(BridgeDriver::MOTOR_B);
- break;
- case 'C':
- case 'c':
- setMotor(BridgeDriver::MOTOR_C);
- break;
- case 'D':
- case 'd':
- setMotor(BridgeDriver::MOTOR_D);
- break;
- }
- }
-
- //if the first word is not "device" it means that it's trying to perform a function to a specific Motor
- else{
- //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
- char *localname = lineData.word[0]; //just for the sake of following the variable easily and understanding
- char *func = lineData.word[1];
-
- /******************************************************************************/
- /*** <Func: enableBrake> ***/
- /******************************************************************************/
- if (strncmp(func,"enableBrake", 11) == 0){
-
- if (lineData.numWords != 4){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- //Initialize and Convert Parameters
- char *enable = lineData.word[2];
- int enableValue = atoi(enable);
-
- bridges.enableBraking(getMotor(), enableValue);
- }
-
- /******************************************************************************/
- /*** <Func: forceBrake> ***/
- /******************************************************************************/
- else if (strncmp(func,"forceBrake", 10) == 0){
-
- if (lineData.numWords != 3){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- bridges.forceBrake(getMotor());
- }
-
-
- /******************************************************************************/
- /*** <Func: drive> ***/
- /******************************************************************************/
- else if (strncmp(func,"drive", 5) == 0){
-
- lcd.setAddress(0,2);
- lcd.printf("wrd2: %s", lineData.word[1]);
- lcd.setAddress(0,3);
- lcd.printf("TEST2 ");
- wait(1);
-
- if (lineData.numWords != 4){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- //Initialize Parameters
- char *speed = lineData.word[2];
- char *dir = lineData.word[3];
-
- lcd.setAddress(0,3);
- lcd.printf("TEST3 ");
-
- //Initialize Convertion Variables if needed
- float speedValue;
- int dirValue = 0;
-
- lcd.setAddress(0,3);
- lcd.printf("TEST4 ");
-
- //Convert string to usable values
- //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
- speedValue = atof(speed) / 100;
-
- lcd.setAddress(0,3);
- lcd.printf("TEST5 ");
-
- if (speedValue <= 0)
- return 0; //Error Out because a value gives no functionality or is wrong
-
- lcd.setAddress(0,3);
- lcd.printf("TEST6 ");
-
- if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0)
- dirValue = -1; //Turn Clockwise
- else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0)
- dirValue = 1; //Turn CounterClockwise
- else
- return 0; //Error Out since the parameter is incorrect
-
- lcd.setAddress(0,3);
- lcd.printf("TEST7 ");
-
- bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
-
- lcd.setAddress(0,3);
- lcd.printf("TEST8 ");
-
- }
- /******************************************************************************/
- /**** <Func: off> ****/
- /******************************************************************************/
- else if (strncmp(func,"off", 3) == 0){
-
- lcd.setAddress(0,2);
- lcd.printf("wrd2: %s", lineData.word[1]);
- lcd.setAddress(0,3);
- lcd.printf("TEST2 ");
- wait(1);
-
- if (lineData.numWords != 2){
- //Error Check, incorrect number of parameter, error out
- return 0;
- }
-
- bridges.drive(getMotor(), 0, 0); //Turn off the Motor
- }
-
- else {
- return 0;
-
- }
- }
-
- return 1;
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DeviceClasses.hpp Thu Sep 18 23:38:11 2014 +0000
@@ -0,0 +1,406 @@
+///*#include "mbed.h"
+//#include "LocalPinNames.h"
+//#include "TextLCD.h"
+//#include "BridgeDriver.h"
+//*/
+//#include "Device.hpp"
+////#include <string>
+//
+///**********************************************************************************************************************************/
+///**********************************************************************************************************************************/
+///*************************** <CLASS: Voltage_Driver> ****************************/
+///**********************************************************************************************************************************/
+///**********************************************************************************************************************************/
+//
+//class Voltage_Driver{
+//
+// public :
+// //uint8_t ch123; //Channel value this device is set to
+// int ch;
+// int test;
+//
+// int interpret();
+// int forceBrake();
+// //int forceFloat();
+// float drive();
+//
+// //Voltage_Driver();
+// void setChannel(int channel){
+// Voltage_Driver::ch = channel;
+// }
+//
+// int getChannel(){
+// return Voltage_Driver::ch;
+// }
+//};
+//
+////Voltage_Driver::Voltage_Driver(void){
+////}
+//
+////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
+//int Voltage_Driver::interpret(){
+// /*
+// lcd.cls(); //clear the display
+// lcd.setAddress(0,3);
+// lcd.printf("Test2 ");
+// lcd.setAddress(0,1);
+// lcd.printf("wrd1: %s", lineData.word[0]);
+// lcd.setAddress(0,2);
+// lcd.printf("wrd2: %s", lineData.word[1]);
+// wait(3);
+//*/
+//
+//
+// string firstWord = lineData.word[0]; //get the first word in the line
+///*
+// lcd.setAddress(0,2);
+// lcd.printf("fW: %s", firstWord);
+// lcd.setAddress(0,3);
+// lcd.printf("ch: %d", ch);
+// wait(3);*/
+//
+// //Initialize the class and device
+// if (firstWord.compare("device") == 0){
+// //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
+//
+// test = 10;
+//
+// if (lineData.numWords != 4){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// string channel = lineData.word[3]; //Parameter is a number
+// int channelValue = 0;
+// sscanf(channel.c_str(), "%d", &channelValue);
+//
+// //Channel Value between 1 to 8
+// if (channelValue >= 1 && channelValue <= 8){
+//
+// setChannel(channelValue);
+// //ch = channelValue;
+//
+// lcd.setAddress(0,2);
+// lcd.printf("fW: %s", firstWord);
+// //lcd.setAddress(0,3);
+// //lcd.printf("ch: %d", getChannel());
+// lcd.setAddress(0,3);
+// lcd.printf("test: %d", test);
+// //lcd.setAddress(0,3);
+// //lcd.printf("chString: %s", chString);
+// wait(2);
+//
+// switch (channelValue){
+// case 1:
+// case 2:
+// bridges.enablePwm(BridgeDriver::MOTOR_A, 0);
+// break;
+// case 3:
+// case 4:
+// bridges.enablePwm(BridgeDriver::MOTOR_B, 0);
+// break;
+// case 5:
+// case 6:
+// bridges.enablePwm(BridgeDriver::MOTOR_C, 0);
+// break;
+// case 7:
+// case 8:
+// bridges.enablePwm(BridgeDriver::MOTOR_D, 0);
+// break;
+// }
+// /* lcd.setAddress(0,2);
+// lcd.printf("AchannelVal: %d", channelValue);
+// lcd.setAddress(0,3);
+// lcd.printf("Ach: %d", ch);
+// wait(3);*/
+//
+// }
+// else{
+// //ERROR Out
+// //Unacceptable channel, or conversion function failed when converting string to int
+// }
+// }
+//
+// //if the first word is not "device" it means that it's trying to perform a function to a specific Motor
+// else{
+//
+//
+//
+// //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
+// string localname = lineData.word[0]; //just for the sake of following the variable easily and understanding
+// string func = lineData.word[1];
+//
+// /******************************************************************************/
+// /*** <Func: forceBrake> ***/
+// /******************************************************************************/
+// if (func.compare("forceBrake") == 0){
+//
+// if (lineData.numWords != 3){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// bridges.forceBrake(ch);
+// }
+//
+//
+// /******************************************************************************/
+// /*** <Func: drive> ***/
+// /******************************************************************************/
+// else if (func.compare("drive") == 0){
+// //Order of Line: Local_Name, drive
+//
+// lcd.setAddress(0,0);
+// lcd.printf("lclN: %s ", localname);
+// lcd.setAddress(0,2);
+// lcd.printf("func: %s ", func);
+// //lcd.setAddress(0,3);
+// //lcd.printf("3ch: %d ", getChannel());
+// lcd.setAddress(0,3);
+// lcd.printf("test: %d", test);
+// //lcd.setAddress(0,3);
+// //lcd.printf("chString: %s ", chString);
+// wait(3);
+//
+// if (lineData.numWords != 2){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+// /*
+// int State = 2;
+// int channelValue = atoi(chString);
+// State = bridges.drive(channelValue, 1); //Turn channel on
+// lcd.setAddress(0,2);
+// lcd.printf("TEST ch: %d, State: %d", channelValue, State);
+// wait(3);*/
+//
+// }
+//
+//
+// /******************************************************************************/
+// /**** <Func: off> ****/
+// /******************************************************************************/
+// else if (func.compare("drive") == 0){
+// //Order of Line: Local_Name, drive, duration
+//
+// if (lineData.numWords != 2){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// bridges.drive(getChannel(), 0); //Turn channel off
+// }
+//
+// else {
+// return 0;
+//
+// }
+// }
+// /*
+// lcd.setAddress(0,3);
+// lcd.printf("ENDch: %d ", getChannel());
+// wait(3);*/
+//
+// return 1;
+//}
+//
+//
+//
+///**********************************************************************************************************************************/
+///**********************************************************************************************************************************/
+///******************************** <CLASS: Motor> ********************************/
+///**********************************************************************************************************************************/
+///**********************************************************************************************************************************/
+//
+//class Motor{
+//
+// enum BridgeDriver::Motors motor;
+//
+// public :
+// int interpret();
+// void enableBrake();
+// int forceBrake();
+// //int forceFloat();
+// float drive();
+//
+//
+// void setMotor(BridgeDriver::Motors selectedMotor){
+// motor = selectedMotor;
+// }
+//
+// enum BridgeDriver::Motors getMotor(){
+// return motor;
+// }
+//
+//};
+//
+////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
+//int Motor::interpret(){
+// /*
+// lcd.cls(); //clear the display
+// lcd.setAddress(0,3);
+// lcd.printf("Test2 ");
+// lcd.setAddress(0,1);
+// lcd.printf("wrd1: %s", lineData.word[0]);
+// lcd.setAddress(0,2);
+// lcd.printf("wrd2: %s", lineData.word[1]);
+// wait(3);
+//*/
+// /*
+// lcd.setAddress(0,2);
+// lcd.printf("wrd1: %s", lineData.word[0]);
+// lcd.setAddress(0,3);
+// lcd.printf("TEST1 ");
+// wait(1);*/
+//
+// string firstWord = lineData.word[0]; //splits the str based on a space delimeter
+//
+// if (firstWord.compare("device") == 0){
+// //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
+//
+// if (lineData.numWords != 4){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// string channel = lineData.word[3]; //Parameter is a single character, so dereference the point to the word
+//
+// if ((channel.compare("A") == 0) || (channel.compare("a") == 0))
+// setMotor(BridgeDriver::MOTOR_A);
+// else if ((channel.compare("B") == 0) || (channel.compare("b") == 0))
+// setMotor(BridgeDriver::MOTOR_B);
+// else if ((channel.compare("C") == 0) || (channel.compare("c") == 0))
+// setMotor(BridgeDriver::MOTOR_C);
+// else if ((channel.compare("D") == 0) || (channel.compare("d") == 0))
+// setMotor(BridgeDriver::MOTOR_D);
+// }
+//
+// //if the first word is not "device" it means that it's trying to perform a function to a specific Motor
+// else{
+// //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
+// //char localname[15] = lineData.word[0]; //just for the sake of following the variable easily and understanding
+// string func = lineData.word[1];
+//
+// /******************************************************************************/
+// /*** <Func: enableBrake> ***/
+// /******************************************************************************/
+// if (func.compare("enableBrake") == 0){
+//
+// if (lineData.numWords != 4){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// //Initialize and Convert Parameters
+// string enable = lineData.word[2];
+// int enableValue = 0;
+// sscanf(enable.c_str(), "%d", &enableValue);
+//
+// bridges.enableBraking(getMotor(), enableValue);
+// }
+//
+// /******************************************************************************/
+// /*** <Func: forceBrake> ***/
+// /******************************************************************************/
+// else if (func.compare("forceBrake") == 0){
+//
+// if (lineData.numWords != 3){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// bridges.forceBrake(getMotor());
+// }
+//
+//
+// /******************************************************************************/
+// /*** <Func: drive> ***/
+// /******************************************************************************/
+// else if (func.compare("drive") == 0){
+//
+// /*lcd.setAddress(0,2);
+// lcd.printf("wrd2: %s", lineData.word[1]);
+// lcd.setAddress(0,3);
+// lcd.printf("TEST2 ");
+// wait(1);*/
+//
+// if (lineData.numWords != 4){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// //Initialize Parameters
+// string speed = lineData.word[2];
+// string dir = lineData.word[3];
+//
+// //Initialize Convertion Variables if needed
+// float speedValue;
+// int dirValue = 0;
+//
+// //Convert string to usable values
+// //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
+// sscanf(speed.c_str(), "%f", &speedValue);
+// speedValue = speedValue / 100;
+//
+// if (speedValue <= 0)
+// return 0; //Error Out because a value gives no functionality or is wrong
+//
+// if (dir.compare("CC") == 0 || dir.compare("cc") == 0)
+// dirValue = -1; //Turn Clockwise
+// else if (dir.compare("C") == 0 || dir.compare("c") == 0)
+// dirValue = 1; //Turn CounterClockwise
+// else
+// return 0; //Error Out since the parameter is incorrect
+//
+// bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
+//
+// }
+// /******************************************************************************/
+// /**** <Func: off> ****/
+// /******************************************************************************/
+// else if (func.compare("off") == 0){
+//
+// /*lcd.setAddress(0,2);
+// lcd.printf("wrd2: %s", lineData.word[1]);
+// lcd.setAddress(0,3);
+// lcd.printf("TEST2 ");
+// wait(1);*/
+//
+// if (lineData.numWords != 2){
+// //Error Check, incorrect number of parameter, error out
+// return 0;
+// }
+//
+// bridges.drive(getMotor(), 0, 0); //Turn off the Motor
+// }
+//
+// else {
+// return 0;
+//
+// }
+// }
+//
+// return 1;
+//}
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Initialization.cpp Thu Sep 18 23:38:11 2014 +0000
@@ -0,0 +1,69 @@
+#include "Initialization.hpp"
+#include "Motor.hpp"
+#include "VoltageDriver.hpp"
+
+//Initializations
+Timer timer; // general purpose timer
+I2C i2c( P0_10, P0_11 ); // I2C bus (SDA, SCL)
+BridgeDriver bridges(&i2c, 1); // Bridge
+TextLCD_I2C lcd( &i2c, MCP23008_SA0, TextLCD::LCD20x4 ); // LCD
+SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // the pinout on the mbed LPC1768
+
+DigitalIn killSw(KILL);
+
+const int MAX_LINE_LENGTH = 100;
+
+//struct Line lineData;
+
+Device* Device::newDevice(int deviceFound, string _name, Line lineData){
+
+ switch (Device_Map[deviceFound]){
+ case MOTOR:
+ //Device::setName(_name);
+ //Device::setType(MOTOR);
+ return new Motor(lineData);
+ break;
+ case VOLTAGE_DRIVER:
+ //setName(_name);
+ //setType(MOTOR);
+ return new VoltageDriver(lineData);
+ break;
+ }
+}
+/*
+void Device::setName(string _name){
+ this->name = _name;
+}
+void Device::setType(DeviceType _type){
+ this->type = _type;
+}*/
+
+const char *DeviceNames[] = {"MOTOR", "VOLTAGE_DRIVER"};
+int numDevices = sizeof(DeviceNames)/sizeof(DeviceNames[0]);
+int currNumDevices = 0;
+
+vector<Device*> devices;
+//Device devices[15];
+
+void addDevice(int deviceFound, Line &lineData){
+
+ devices[currNumDevices] = Device::newDevice(deviceFound, lineData.word[1], lineData);
+ currNumDevices++;
+}
+
+void fullInit() {
+
+ killSw.mode(PullUp);
+ initLCD();
+}
+
+void initLCD(void) {
+
+ i2c.frequency(1000000);
+ lcd.setBacklight(TextLCD::LightOn);
+ wait(.6);
+ lcd.cls(); //clear the display
+ lcd.setAddress(0,0);
+ lcd.printf("LCD Initialized");
+}
+
--- a/Initialization.hpp Tue Sep 16 18:27:41 2014 +0000
+++ b/Initialization.hpp Thu Sep 18 23:38:11 2014 +0000
@@ -7,48 +7,69 @@
#include "FrontPanelButtons.h"
#include "TextLCD.h"
#include "SDFileSystem.h"
+#include <string>
+#include <vector>
//Initializations
-Timer timer; // general purpose timer
-I2C i2c( P0_10, P0_11 ); // I2C bus (SDA, SCL)
-BridgeDriver bridges(&i2c, 1); // Bridge
-TextLCD_I2C lcd( &i2c, MCP23008_SA0, TextLCD::LCD20x4 ); // LCD
-SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // the pinout on the mbed LPC1768
-
-DigitalIn killSw(KILL);
-
-extern const int MAX_LINE_LENGTH = 100;
-
-void initLCD(void);
+extern Timer timer; // general purpose timer
+extern I2C i2c; // I2C bus (SDA, SCL)
+extern BridgeDriver bridges; // Bridge
+extern TextLCD_I2C lcd; // LCD
+extern SDFileSystem sd; // the pinout on the mbed LPC1768
-void fullInit() {
-
- killSw.mode(PullUp);
- initLCD();
-}
+extern DigitalIn killSw;
+
+extern const int MAX_LINE_LENGTH;
+
+void fullInit();
-void initLCD(void) {
-
- i2c.frequency(1000000);
- lcd.setBacklight(TextLCD::LightOn);
- wait(.6);
- lcd.cls(); //clear the display
- lcd.setAddress(0,0);
- lcd.printf("LCD Initialized");
-}
+void initLCD(void);
struct Line{
- char *fullLine; //full line, starting from the beginning
- char *currPartLine; //current line, meaning that it only knows whatever has not been used in the line
+ //string fullLine; //full line, starting from the beginning
int lineNumber; //current line number in the program txt file that is running
- char word[15][MAX_LINE_LENGTH]; //array of words from the line of text, assuming no more than 15 words will be in any given line
+ //vector<string> word;
+ string word[15]; //array of words from the line of text, assuming no more than 15 words will be in any given line
//in this initialization there are 15 string (pointers) of size MAX_LINE_LENGTH each
int numWords; //Number of words in the given line
int lineAddress;
};
+//
extern struct Line lineData;
+
+
+
+enum DeviceType{MOTOR, VOLTAGE_DRIVER};
+static const enum DeviceType Device_Map[] = {MOTOR, VOLTAGE_DRIVER};
+extern const char *DeviceNames[];
+extern int numDevices;
+extern int currNumDevices;
+
+
+class Device{
+
+ public:
+ string name;
+ enum DeviceType type;
+ static Device* newDevice(int, string, Line);
+ //void setName(string);
+ //void setType(DeviceType);
+ virtual int interpret(Line&) = 0;
+ /*{
+ lcd.setAddress(0,3);
+ lcd.printf("CHECK Device ");
+ wait(2);
+ return 0;
+ }*/
+};
+
+extern vector<Device*> devices; //Initialize array of devices, initially assume 15 devices will be used (will expand as needed)
+//extern Device devices[];
+
+void addDevice(int, Line);
+
#endif
\ No newline at end of file
--- a/Motor.cpp Tue Sep 16 18:27:41 2014 +0000
+++ b/Motor.cpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,181 +1,224 @@
-//#include "Motor.hpp"
+#include "Motor.hpp"
//#include "mbed.h"
//#include "LocalPinNames.h"
//#include "BridgeDriver.h"
-//
-////Constructor
-//Motor::Motor(){
-// /*
-// //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
-// if (lineData.numWords != 4){
-// //Error Check, incorrect number of parameter, error out
-// //return 0;
-// }*/
-//
-// char channel = *lineData.word[3]; //Parameter is a single character, so dereference the point to the word
-//
-// switch (channel){
-// case 'A':
-// case 'a':
-// this->motor = BridgeDriver::MOTOR_A;
-// break;
-// case 'B':
-// case 'b':
-// this->motor = BridgeDriver::MOTOR_B;
-// break;
-// case 'C':
-// case 'c':
-// this->motor = BridgeDriver::MOTOR_C;
-// break;
-// case 'D':
-// case 'd':
-// this->motor = BridgeDriver::MOTOR_D;
-// break;
-// }
-//}
-//
-//
-//enum BridgeDriver::Motors Motor::getMotor(){
-// return this->motor;
-//}
-//
-////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
-//int Motor::interpret(){
-//
-//
-// //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
-// char *localname = lineData.word[0]; //just for the sake of following the variable easily and understanding
-// char *func = lineData.word[1];
-//
-// /******************************************************************************/
-// /*** <Func: enableBrake> ***/
-// /******************************************************************************/
-// if (strncmp(func,"enableBrake", 11) == 0){
-//
-// if (lineData.numWords != 4){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// //Initialize and Convert Parameters
-// char *enable = lineData.word[2];
-// int enableValue = atoi(enable);
-//
-// bridges.enableBraking(getMotor(), enableValue);
-// }
-//
-// /******************************************************************************/
-// /*** <Func: forceBrake> ***/
-// /******************************************************************************/
-// else if (strncmp(func,"forceBrake", 10) == 0){
-//
-// if (lineData.numWords != 3){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.forceBrake(getMotor());
-// }
-//
-//
-// /******************************************************************************/
-// /*** <Func: drive> ***/
-// /******************************************************************************/
-//
-//
-// /******************************************************************************/
-// /**** <Func: off> ****/
-// /******************************************************************************/
-// else if (strncmp(func,"off", 3) == 0){
-//
-// if (lineData.numWords != 2){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.drive(getMotor(), 0, 0); //Turn off the Motor
-// }
-//
-// else {
-// return 0;
-//
-// }
-//
-//
-// return 1;
-//}
-//
-//
-//
-//
-//
-//
-//
-///*
-//int Motor::enableBrake(){
-//
-// if (lineData.numWords != 4){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// //Initialize and Convert Parameters
-// char *enable = lineData.word[2];
-// int enableValue = atoi(enable);
-//
-// //If the atoi returned anything besides 0, it worked properly
-// if (enableValue)
-// bridges.enableBraking(getMotor(), enableValue);
-// else{
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// return 1;
-//}
-//
-//int Motor::forceBrake(){
-//
-// if (lineData.numWords != 3){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.forceBrake(getMotor());
-//}
-//
-//int Motor::drive(){
-//
-// if (lineData.numWords != 4){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// //Initialize Parameters
-// char *speed = lineData.word[2];
-// char *dir = lineData.word[3];
-//
-// //Initialize Convertion Variables if needed
-// float speedValue;
-// int dirValue = 0;
-//
-// //Convert string to usable values
-// //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
-// speedValue = atof(speed) / 100;
-//
-// if (speedValue <= 0)
-// return 0; //Error Out because a value gives no functionality or is wrong
-//
-// if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0)
-// dirValue = -1; //Turn Clockwise
-// else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0)
-// dirValue = 1; //Turn CounterClockwise
-// else
-// return 0; //Error Out since the parameter is incorrect
-//
-// bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
-//}
-//*/
-//
-//
-//
\ No newline at end of file
+
+//Constructor
+Motor::Motor(Line lineData){
+ /*
+ //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ //return 0;
+ }*/
+
+ /*lcd.setAddress(0,3);
+ lcd.printf("CHECK Motor Construct");
+ wait(2);*/
+
+ string channel = lineData.word[3]; //Parameter is a single character, so dereference the point to the word
+
+ if ((channel.compare("A") == 0) || (channel.compare("a") == 0))
+ this->motor = BridgeDriver::MOTOR_A;
+ else if ((channel.compare("B") == 0) || (channel.compare("b") == 0))
+ this->motor = BridgeDriver::MOTOR_B;
+ else if ((channel.compare("C") == 0) || (channel.compare("c") == 0))
+ this->motor = BridgeDriver::MOTOR_C;
+ else if ((channel.compare("D") == 0) || (channel.compare("d") == 0))
+ this->motor = BridgeDriver::MOTOR_D;
+}
+
+
+enum BridgeDriver::Motors Motor::getMotor(){
+ return this->motor;
+}
+
+//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
+int Motor::interpret(Line &lineData){
+ /*
+ lcd.setAddress(0,2);
+ lcd.printf("CHECK Motor 123456");
+ lcd.setAddress(0,3);
+ lcd.printf("func: %s ", lineData.word[1]);
+ wait(2);*/
+
+ //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
+ //char localname[15] = lineData.word[0]; //just for the sake of following the variable easily and understanding
+ string func = lineData.word[1];
+
+ /******************************************************************************/
+ /*** <Func: enableBrake> ***/
+ /******************************************************************************/
+ if (func.compare("enableBrake") == 0){
+
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ //Initialize and Convert Parameters
+ string enable = lineData.word[2];
+ int enableValue = 0;
+ sscanf(enable.c_str(), "%d", &enableValue);
+
+ bridges.enableBraking(getMotor(), enableValue);
+ }
+
+ /******************************************************************************/
+ /*** <Func: forceBrake> ***/
+ /******************************************************************************/
+ else if (func.compare("forceBrake") == 0){
+
+ if (lineData.numWords != 3){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ bridges.forceBrake(getMotor());
+ }
+
+
+ /******************************************************************************/
+ /*** <Func: drive> ***/
+ /******************************************************************************/
+ else if (func.compare("drive") == 0){
+
+ /*lcd.setAddress(0,2);
+ lcd.printf("wrd2: %s", lineData.word[1]);
+ lcd.setAddress(0,3);
+ lcd.printf("TEST2 ");
+ wait(2);*/
+
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ //Initialize Parameters
+ string speed = lineData.word[2];
+ string dir = lineData.word[3];
+
+ //Initialize Convertion Variables if needed
+ float speedValue;
+ int dirValue = 0;
+
+ //Convert string to usable values
+ //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
+ sscanf(speed.c_str(), "%f", &speedValue);
+ speedValue = speedValue / 100;
+
+ if (speedValue <= 0)
+ return 0; //Error Out because a value gives no functionality or is wrong
+
+ if (dir.compare("CC") == 0 || dir.compare("cc") == 0)
+ dirValue = -1; //Turn Clockwise
+ else if (dir.compare("C") == 0 || dir.compare("c") == 0)
+ dirValue = 1; //Turn CounterClockwise
+ else
+ return 0; //Error Out since the parameter is incorrect
+
+
+ bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
+
+ }
+ /******************************************************************************/
+ /**** <Func: off> ****/
+ /******************************************************************************/
+ else if (func.compare("off") == 0){
+
+ /*lcd.setAddress(0,2);
+ lcd.printf("wrd2: %s", lineData.word[1]);
+ lcd.setAddress(0,3);
+ lcd.printf("TEST2 ");
+ wait(1);*/
+
+ if (lineData.numWords != 2){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ bridges.drive(getMotor(), 0, 0); //Turn off the Motor
+ }
+
+ else {
+ return 0;
+
+ }
+
+ return 1;
+}
+
+
+
+
+
+
+
+/*
+int Motor::enableBrake(){
+
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ //Initialize and Convert Parameters
+ char *enable = lineData.word[2];
+ int enableValue = atoi(enable);
+
+ //If the atoi returned anything besides 0, it worked properly
+ if (enableValue)
+ bridges.enableBraking(getMotor(), enableValue);
+ else{
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ return 1;
+}
+
+int Motor::forceBrake(){
+
+ if (lineData.numWords != 3){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ bridges.forceBrake(getMotor());
+}
+
+int Motor::drive(){
+
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ //Initialize Parameters
+ char *speed = lineData.word[2];
+ char *dir = lineData.word[3];
+
+ //Initialize Convertion Variables if needed
+ float speedValue;
+ int dirValue = 0;
+
+ //Convert string to usable values
+ //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
+ speedValue = atof(speed) / 100;
+
+ if (speedValue <= 0)
+ return 0; //Error Out because a value gives no functionality or is wrong
+
+ if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0)
+ dirValue = -1; //Turn Clockwise
+ else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0)
+ dirValue = 1; //Turn CounterClockwise
+ else
+ return 0; //Error Out since the parameter is incorrect
+
+ bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
+}
+*/
+
+
+
\ No newline at end of file
--- a/Motor.hpp Tue Sep 16 18:27:41 2014 +0000
+++ b/Motor.hpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,21 +1,22 @@
-//#ifndef MOTOR_HPP
-//#define MOTOR_HPP
-//
+#ifndef MOTOR_HPP
+#define MOTOR_HPP
+
//#include "Device.hpp"
-//
-//class Motor: public Device{
-//
-// private:
-// enum BridgeDriver::Motors motor;
-//
-// public:
-// Motor();
-// int interpret();
-// enum BridgeDriver::Motors getMotor();
-// //void enableBrake();
-// //int forceBrake();
-// //int forceFloat();
-// //float drive();
-//};
-//
-//#endif
\ No newline at end of file
+#include "Initialization.hpp"
+
+class Motor: public Device{
+
+ private:
+ enum BridgeDriver::Motors motor;
+
+ public:
+ Motor(Line);
+ int interpret(Line&);
+ enum BridgeDriver::Motors getMotor();
+ //void enableBrake();
+ //int forceBrake();
+ //int forceFloat();
+ //float drive();
+};
+
+#endif
\ No newline at end of file
--- a/TextFile.h Tue Sep 16 18:27:41 2014 +0000
+++ b/TextFile.h Thu Sep 18 23:38:11 2014 +0000
@@ -1,3 +1,6 @@
+#ifndef TEXTFILE_HPP
+#define TEXTFILE_HPP
+
#include "mbed.h"
#include "TextLCD.h"
#include "SDFileSystem.h"
@@ -7,10 +10,17 @@
#include <vector>
using std::string;
+#include <iostream>
+#include <istream>
+#include <ostream>
+#include <iterator>
+#include <sstream>
+
+
//vector<string> filenames; //filenames are stored in a vector string
-vector<string> readFileNames(char *);
+vector<string> readFileNames(char []);
int getFileNamesWithoutExt(string[], vector<string> &);
string getPreviousLine(istream &);
@@ -40,28 +50,29 @@
/*** <getFileNamesWithoutExt> ***/
/******************************************************************************/
// save filename strings to array from vector using an iterator
-int getFileNamesWithoutExt(string fileNames[], vector<string> &filenames) {
+int getFileNamesWithoutExt(vector<string> &textFiles, vector<string> filenames) {
//Cycle through all files listed in the directoy (strings in the vector list)
int n = 0;
for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++)
{
- //Convert the Vector found String to a char for use in the strtok function
- char temp[50];
- if((*it).size() < 100)
- strcpy(temp, (*it).c_str());
-
- // Retrieve the fileName and fileExtension by splitting the string
- char *fileName;
- char *fileExtension;
- fileName = strtok(temp, ".");
- fileExtension = strtok(NULL, ".");
+
+ vector<string> fileName; //filename[0] = filename, filename[1] = extension
+
+ stringstream ss;
+ ss << (*it);
- // if the file is a .txt file, then save it to the array
- if (strncmp(fileExtension,"txt", 3) == 0) {
- fileNames[n] = fileName;
+ //parse the array based on a '.' delimeter, effectively getting the file name and extension
+ string item;
+ while (getline(ss, item, '.')) {
+ fileName.push_back(item);
+ }
+
+ // if the fileName vector has two items (a name and an extension) and is a .txt file, then save it to the array
+ if (fileName.size() == 2 && fileName[1].compare("txt") == 0) {
+ textFiles.push_back(fileName[0]);
n++;
- }
+ }
}
return n; //Return the number of txt files that were found in the directory
@@ -72,45 +83,44 @@
/******************************************************************************/
//
-void getNextLine(FILE *selectedFile) {
+void getNextLine(FILE *selectedFile, Line &lineData) {
lineData.lineAddress = ftell(selectedFile);
- char *newLine;
- newLine = new char[MAX_LINE_LENGTH];
+ //char *newLine;
+ //getline(selectedFile, newLine);
+ char newLine[MAX_LINE_LENGTH];
fgets(newLine, MAX_LINE_LENGTH, selectedFile);
- lineData.fullLine = newLine;
- lineData.lineNumber++;
+ string tempString(newLine);
+ //lineData.fullLine = newLine;
+ lineData.lineNumber++;
+
+ stringstream newLineStrm(newLine);
+ //string tempWord = strtok(cpyLine, " ");
- //Create a copy of the line, so that we don't effect the full line when we're extracting the words
- char *cpyLine;
- cpyLine = new char[MAX_LINE_LENGTH];
- strcpy(cpyLine, newLine);
-
- char *tempWord;
- tempWord = new char;
+ istream_iterator<string> it(newLineStrm);
+ istream_iterator<string> end;
+ vector<string> results(it, end);
+ copy(results.begin(), results.end(), lineData.word);
- int numWords = 0;
+ lineData.numWords = results.size(); //Record the number of words in the line
- tempWord = strtok(cpyLine, " ");
+ results.erase(results.begin(), results.end());
+
+ /*
+ int numWords = 0;
while (tempWord != NULL){
strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array
numWords++; //add the number of words first, since we've already got one word, this helps for the way the while loop works too
tempWord = strtok(NULL, " ");
- }
+ }*/
- lineData.numWords = numWords; //Record the number of words in the line
-
lcd.setAddress(0,1);
lcd.printf("Current Line#: %d ", lineData.lineNumber);
-
- delete[] tempWord;
- delete[] newLine;
- delete[] cpyLine;
/*
lcd.cls(); //clear the display
@@ -126,9 +136,9 @@
}
+#endif
-
--- a/VoltageDriver.cpp Tue Sep 16 18:27:41 2014 +0000
+++ b/VoltageDriver.cpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,107 +1,127 @@
-//#include "VoltageDriver.hpp"
+#include "VoltageDriver.hpp"
//#include "mbed.h"
//#include "LocalPinNames.h"
//#include "BridgeDriver.h"
-//
-////Constructor
-//VoltageDriver::VoltageDriver(){
-//
-// //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
-// if (lineData.numWords != 4){
-// //Error Check, incorrect number of parameter, error out
-// //return 0;
-// }
-//
-// char *channel = lineData.word[3]; //Parameter is a number
-// int channelValue = atoi(channel);
-//
-// //Channel Value between 1 to 8
-// if (channelValue >= 1 && channelValue <= 8){
-//
-// switch (channelValue){
-// case 1:
-// case 2:
-// bridges.enablePwm(BridgeDriver::MOTOR_A, 0);
-// break;
-// case 3:
-// case 4:
-// bridges.enablePwm(BridgeDriver::MOTOR_B, 0);
-// break;
-// case 5:
-// case 6:
-// bridges.enablePwm(BridgeDriver::MOTOR_C, 0);
-// break;
-// case 7:
-// case 8:
-// bridges.enablePwm(BridgeDriver::MOTOR_D, 0);
-// break;
-// }
-// }
-// else{
-// //ERROR Out
-// //Unacceptable channel, or conversion function failed when converting string to int
-// }
-//}
-//
-//
-//int VoltageDriver::getChannel(){
-// return this->channel;
-//}
-//
-//
-////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
-//int VoltageDriver::interpret(){
-//
-// //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
-// char *func = lineData.word[1];
-//
-// /******************************************************************************/
-// /*** <Func: forceBrake> ***/
-// /******************************************************************************/
-// if (strncmp(func,"forceBrake", 10) == 0){
-//
-// if (lineData.numWords != 3){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.forceBrake(this->channel);
-// }
-//
-//
-// /******************************************************************************/
-// /*** <Func: drive> ***/
-// /******************************************************************************/
-// else if (strncmp(func,"drive", 5) == 0){
-// //Order of Line: Local_Name, drive
-//
-// if (lineData.numWords != 2){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.drive(this->channel, 1); //Turn channel on
-//
-// }
-//
-//
-// /******************************************************************************/
-// /**** <Func: off> ****/
-// /******************************************************************************/
-// else if (strncmp(func,"drive", 5) == 0){
-// //Order of Line: Local_Name, drive, duration
-//
-// if (lineData.numWords != 2){
-// //Error Check, incorrect number of parameter, error out
-// return 0;
-// }
-//
-// bridges.drive(getChannel(), 0); //Turn channel off
-// }
-//
-// else {
-// return 0;
-// }
-//
-// return 1;
-//}
\ No newline at end of file
+
+//Constructor
+VoltageDriver::VoltageDriver(Line lineData){
+
+ //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
+ if (lineData.numWords != 4){
+ //Error Check, incorrect number of parameter, error out
+ //return 0;
+ }
+
+ string channelstr = lineData.word[3]; //Parameter is a number
+ //int channelValue = 0;
+ sscanf(channelstr.c_str(), "%d", &channel);
+
+ //Channel Value between 1 to 8
+ if (channel >= 1 && channel <= 8){
+
+ switch (channel){
+ case 1:
+ case 2:
+ bridges.enablePwm(BridgeDriver::MOTOR_A, 0);
+ break;
+ case 3:
+ case 4:
+ bridges.enablePwm(BridgeDriver::MOTOR_B, 0);
+ break;
+ case 5:
+ case 6:
+ bridges.enablePwm(BridgeDriver::MOTOR_C, 0);
+ break;
+ case 7:
+ case 8:
+ bridges.enablePwm(BridgeDriver::MOTOR_D, 0);
+ break;
+ }
+ }
+ else{
+ //ERROR Out
+ //Unacceptable channel, or conversion function failed when converting string to int
+ }
+}
+
+
+int VoltageDriver::getChannel(){
+ return this->channel;
+}
+
+
+//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
+int VoltageDriver::interpret(Line &lineData){
+
+
+ //order of line: local_name, function_name, param1, param2, param3,.......
+ //string localname = linedata.word[0]; //just for the sake of following the variable easily and understanding
+ string func = lineData.word[1];
+
+ /******************************************************************************/
+ /*** <func: forcebrake> ***/
+ /******************************************************************************/
+ if (func.compare("forcebrake") == 0){
+
+ if (lineData.numWords != 3){
+ //error check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ bridges.forceBrake(channel);
+ }
+
+
+ /******************************************************************************/
+ /*** <func: drive> ***/
+ /******************************************************************************/
+ else if (func.compare("drive") == 0){
+ //order of line: local_name, drive
+
+// lcd.setAddress(0,0);
+// lcd.printf("lcln: %s ", localname);
+ lcd.setAddress(0,2);
+ lcd.printf("func: %s ", func);
+ //lcd.setAddress(0,3);
+ //lcd.printf("3ch: %d ", getchannel());
+// lcd.setAddress(0,3);
+// lcd.printf("test: %d", test);
+ //lcd.setAddress(0,3);
+ //lcd.printf("chstring: %s ", chstring);
+ wait(3);
+
+ if (lineData.numWords != 2){
+ //error check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ //int state = 2;
+ bridges.drive(channel, 1); //turn channel on
+ /*lcd.setaddress(0,2);
+ lcd.printf("test ch: %d, state: %d", channelvalue, state);
+ wait(3);*/
+
+ }
+
+
+ /******************************************************************************/
+ /**** <func: off> ****/
+ /******************************************************************************/
+ else if (func.compare("drive") == 0){
+ //order of line: local_name, drive, duration
+
+ if (lineData.numWords != 2){
+ //error check, incorrect number of parameter, error out
+ return 0;
+ }
+
+ bridges.drive(getChannel(), 0); //turn channel off
+ }
+
+ else {
+ return 0;
+
+ }
+
+ return 1;
+}
\ No newline at end of file
--- a/VoltageDriver.hpp Tue Sep 16 18:27:41 2014 +0000
+++ b/VoltageDriver.hpp Thu Sep 18 23:38:11 2014 +0000
@@ -1,21 +1,22 @@
-//#ifndef VOLTAGEDRIVER_HPP
-//#define VOLTAGEDRIVER_HPP
-//
+#ifndef VOLTAGEDRIVER_HPP
+#define VOLTAGEDRIVER_HPP
+
//#include "Device.hpp"
-//
-//class VoltageDriver: public Device{
-//
-// private:
-// int channel;
-//
-// public:
-// VoltageDriver();
-// int interpret();
-// int getChannel();
-// //void enableBrake();
-// //int forceBrake();
-// //int forceFloat();
-// //float drive();
-//};
-//
-//#endif
\ No newline at end of file
+#include "Initialization.hpp"
+
+class VoltageDriver: public Device{
+
+ private:
+ int channel;
+
+ public:
+ VoltageDriver(Line);
+ int interpret(Line&);
+ int getChannel();
+ //void enableBrake();
+ //int forceBrake();
+ //int forceFloat();
+ //float drive();
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp Tue Sep 16 18:27:41 2014 +0000
+++ b/main.cpp Thu Sep 18 23:38:11 2014 +0000
@@ -7,22 +7,22 @@
#include "SDFileSystem.h"
#include "Initialization.hpp"
//#include "DeviceClasses.h"
-#include "Device.hpp"
-//#include "Motor.hpp"
-//#include "VoltageDriver.hpp"
+//#include "Device.hpp"
+#include "Motor.hpp"
+#include "VoltageDriver.hpp"
#include "TextFile.h"
#include <stdio.h>
#include <string>
#include <stdlib.h>
-#include <iostream>
#include <fstream>
#include <vector>
using std::string;
//const int MAX_LINE_LENGTH = 100;
-struct Line lineData;
-//Device devices[15];
-DeviceData devices[15];
+//struct Line lineData;
+
+
+//DeviceData devices[15];
FrontPanelButtons buttons(&i2c);
@@ -36,19 +36,20 @@
-int interpretCommand(FILE *);
-int loopCommand(FILE *);
+int interpretCommand(FILE *, Line &);
+int loopCommand(FILE *, Line &);
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
/*************************** <FUNCTION: resetLineData> **************************/
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
-void resetLineData(){
+void resetLineData(Line &lineData){
- lineData.fullLine = NULL;
+ //lineData.fullLine.clear();
lineData.lineNumber = 0;
lineData.numWords = 0;
+ lineData.lineAddress = 0;
}
@@ -59,7 +60,7 @@
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
-int cyclePrograms(string files[], int SIZE, int currIndex, int direction){
+int cyclePrograms(vector<string> files, int SIZE, int currIndex, int direction){
int nextIndex = 0;
switch(direction){
@@ -112,84 +113,41 @@
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
-int loopCommand(FILE *selectedFile){
+int loopCommand(FILE *selectedFile, Line &lineData){
- char *loopCondition = lineData.word[1];
- int loopConditionValue = atoi(loopCondition);
- int loopStartAddress = 0, loopLineNumber = 0;
- int firstLineOfLoop = 1;
+ //Get the Condition value for number of times to loop
+ string loopCondition = lineData.word[1];
+ int loopConditionValue = 0;
+ sscanf(loopCondition.c_str(), "%d", &loopConditionValue);
- int counter = 0;
+ int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1;
+
+ int counter = 0, checkEnd = 0;
while (counter < loopConditionValue){
- getNextLine(selectedFile);
- /*lcd.setAddress(0,2);
- lcd.printf("wrd1: %s", lineData.word[0]);
- lcd.setAddress(0,3);
- lcd.printf("TEST012 ");*/
-
- int checkEnd = interpretCommand(selectedFile);
- /*lcd.setAddress(0,3);
- lcd.printf("TEST022 ");
- wait(2);*/
+ getNextLine(selectedFile, lineData);
+ checkEnd = interpretCommand(selectedFile, lineData);
if (firstLineOfLoop){
loopStartAddress = lineData.lineAddress; //Save the Line Address
loopLineNumber = lineData.lineNumber; //Save the Line Number
firstLineOfLoop = 0;
- /* lcd.setAddress(0,2);
- lcd.printf("ldLN: %d, ldLA: %d", lineData.lineNumber, lineData.lineAddress);
- lcd.setAddress(0,3);
- lcd.printf("lln: %d, lsa: %d", loopLineNumber, loopStartAddress);
- wait(3);*/
}
//Increase the loop counter and go back to the beginning of the loop
if (checkEnd == 1){
+ counter++;
+
lcd.setAddress(0,2);
- lcd.printf("%dldLN: %d, ldLA: %d", counter, lineData.lineNumber, lineData.lineAddress);
- lcd.setAddress(0,3);
- lcd.printf("%dlln: %d, lsa: %d", counter, loopLineNumber, loopStartAddress);
- wait(3);
-
- counter++;
+ lcd.printf("Loop Cycle %d of %d", counter, loopConditionValue);
+
fseek(selectedFile, loopStartAddress, SEEK_SET);
- lineData.lineNumber = loopLineNumber - 1;
+ lineData.lineNumber = loopLineNumber - 2;
}
}
-
- lcd.setAddress(0,3);
- lcd.printf("TEST1 ");
-
+
return 1;
}
-
- /*
- Make LOOP function
- - All you need to do is remember (val = ftell(fp)) the beginning of the line you're currently reading.
- - If it turns out to be a LOOP call, then you simply send that remembered value to the LOOP function, and it will continue to LOOP back to that position until the condition is met.
-
-
- func LOOP (FILE *fp, int loopStart){
-
- string conditions;
- fseek(fp, loopStart, SEEK_SET);
- fgets(conditions, MAX_LINE_LENGTH, fp);
-
- int numCycles = interpretLoopCondtions();
-
- int i = 0;
- int checkEndLoop = 0;
- while (i < numCycles) {
-
- checkEndLoop = interpretLine(fp); //if end Loop return ____ (1?), return 0 on success
- if (checkEndLoop) //if it's not the end of the loop, the counter will not increase and we'll just interpret the next line
- i++;
- }
- } //loop conditions met, return to main code
-
-
- */
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
@@ -197,14 +155,15 @@
/**********************************************************************************************************************************/
/**********************************************************************************************************************************/
-int interpretCommand(FILE *selectedFile){
-
- if (strncmp(lineData.word[0],"device", 6) == 0){
-
+int interpretCommand(FILE *selectedFile, Line &lineData){
+
+ if (lineData.word[0].compare("device") == 0){
+
int i = 0, deviceFound = -1;
- for (i = 0; i < numDevices; i++)
- if (strncmp(lineData.word[2], DeviceNames[i], strlen(DeviceNames[i])) == 0)
+ for (i = 0; i < numDevices; i++){
+ if (lineData.word[2].compare(DeviceNames[i]))
deviceFound = i;
+ }
//if the device type does not match any known type, error out
if (deviceFound == -1){
@@ -212,13 +171,18 @@
}
//Add device to the array of devices and initialize it
- else
- addDevice(deviceFound);
+ else{
+ //For some reason the enum is indexed weirdly, device-- to send correct value
+ deviceFound--;
+ devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData));
+ devices.back()->name = lineData.word[1];
+ }
}
- else if (strncmp(lineData.word[0],"delay", 5) == 0){
- char *duration = lineData.word[1];
- int durationValue = atoi(duration);
+ else if (lineData.word[0].compare("delay") == 0){
+ string duration = lineData.word[1];
+ int durationValue = 0;
+ sscanf(duration.c_str(), "%d", &durationValue);
if (durationValue){
timer.reset();
@@ -232,27 +196,27 @@
}
}
- else if (strncmp(lineData.word[0],"loop", 4) == 0){
- int checkLoopEnd = loopCommand(selectedFile);
- lcd.setAddress(0,3);
- lcd.printf("TEST2 ");
+ else if (lineData.word[0].compare("loop") == 0){
+ int checkLoopEnd = loopCommand(selectedFile, lineData);
if (checkLoopEnd == 1)
return 1;
}
- else if (strncmp(lineData.word[0],"end", 3) == 0){
- if (strncmp(lineData.word[1],"program", 7) == 0)
+ else if (lineData.word[0].compare("end") == 0){
+ if (lineData.word[1].compare("program") == 0){
return 0;
- else if (strncmp(lineData.word[1],"loop", 4) == 0)
+ }
+ else if (lineData.word[1].compare("loop") == 0){
return 1;
+ }
}
//not a keyword so check if it's a localName for a device
else{
int i = 0, deviceFound = -1;
- for (i = 0; i < currNumDevices; i++){
- if (strncmp(lineData.word[0], devices[i].name, strlen(devices[i].name)) == 0)
+ for (i = 0; i < devices.size(); i++){
+ if (lineData.word[0].compare(devices[i]->name) == 0)
deviceFound = i;
}
@@ -264,8 +228,10 @@
}
//Local Name matches a device, send line to that device in order to process the functionality
- else
- addDevice(deviceFound);//devices[deviceFound].interpret();
+ else{
+ //addDevice(deviceFound);
+ devices[deviceFound]->interpret(lineData);
+ }
}
return -1;
@@ -282,18 +248,16 @@
fullInit(); //Initialize anything that's required to run the code (LCD)
+ struct Line lineData;
+ resetLineData(lineData);
+
/******************************************************************************/
/*** <Get all the Potential Programs> ***/
/******************************************************************************/
int numTextFiles = 0;
- string tempTextFiles[25]; //Assuming Maximum of 25 txt files will be on the SD Card
- lcd.cls(); //clear the display
-
- vector<string> filenames = readFileNames("/sd");
- numTextFiles = getFileNamesWithoutExt(tempTextFiles, filenames);
- string *textFiles = resize_StringArr(tempTextFiles, numTextFiles); //Resize Array
-
- //delete[] tempTextFiles; //free previous array
+ vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card
+ vector<string> filenames = readFileNames("/sd");
+ numTextFiles = getFileNamesWithoutExt(textFiles, filenames);
/******************************************************************************/
@@ -309,7 +273,7 @@
uint8_t lastButState;
lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on
-
+
selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen
while(!fileSelected) {
@@ -338,6 +302,7 @@
strcat(selectedFileFullName, ".txt");
FILE *selectedFile = fopen(selectedFileFullName, "r");
+
lcd.cls(); //clear the display
if(selectedFile == NULL) {
@@ -349,7 +314,7 @@
while(1){
- resetLineData();
+ resetLineData(lineData);
lcd.cls(); //clear the display
lcd.setAddress(0,0);
@@ -364,12 +329,9 @@
while (!endOfFile){
- getNextLine(selectedFile);
- int checkEnd = interpretCommand(selectedFile);
-
- lcd.setAddress(0,3);
- lcd.printf("TEST3 ");
-
+ getNextLine(selectedFile, lineData);
+ int checkEnd = interpretCommand(selectedFile, lineData);
+
if (checkEnd == 0)
endOfFile = 1;
}
@@ -386,10 +348,30 @@
while(!buttons.readBack());
lcd.setAddress(0,1);
- rewind(selectedFile);
+ //rewind(selectedFile);
}
-
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/***** Cycle through txt lines and remember last lines ******/
/*
int running = 0, selectedFile = 0;
@@ -442,9 +424,7 @@
}
}
*/
-
-
-
+
/******* Select the Program txt File ***********/
@@ -487,19 +467,6 @@
}
*/
-
-
-
-
- }
-
-
-
-
-
-
-
-
/*float speed = 0.5;
while(1){