Permalink
Browse files

changed to add-on

  • Loading branch information...
1 parent 274bb52 commit 187db54581d8ac8dcc606559bbb6e1baa511b0c9 @HalfdanJ HalfdanJ committed Jul 19, 2011
File renamed without changes.
@@ -6,7 +6,7 @@
int main( ){
ofAppGlutWindow window;
- ofSetupOpenGL(&window, 100,100, OF_WINDOW); // <-------- setup the GL context
+ ofSetupOpenGL(&window, 500,400, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
File renamed without changes.
@@ -27,8 +27,28 @@ void testApp::draw(){
//--------------------------------------------------------------
void testApp::keyPressed (int key){
if(key == 'v'){
- adlp.sendMessage();
+ unsigned char data[4];
+ data[0] = 0;
+ data[1] = 1;
+ data[2] = 0;
+ data[3] = 2;
+ adlp.sendMessage((ADLPInstruction)0x02, data, 4);
+
+ } if(key == 's'){
+ adlp.sendMessage((ADLPInstruction)stopProgram, nil, 0);
}
+ if(key == 'i'){
+ adlp.sendMessage((ADLPInstruction)0x15, nil, 0);
+ }
+ if(key == 'd'){
+ unsigned char data[4];
+ data[0] = 0;
+ data[1] = 1;
+ data[3] = 0;
+ data[4] = 1;
+ adlp.sendMessage((ADLPInstruction)0x1D, data, 4);
+ }
+
}
//--------------------------------------------------------------
File renamed without changes.
View
@@ -15,52 +15,208 @@ ADLPCom::ADLPCom(){
serial.enumerateDevices();
+ incommingMessageIndex = -1;
+ receivingMessage = false;
+ sendingMessage = false;
+ memset(outgoingFormalities, false, 2);
}
void ADLPCom::update(){
unsigned char bytesReturned;
while(serial.available() > 0){
- (bytesReturned = serial.readByte());
+ bytesReturned = serial.readByte();
+
+ printf("%i: ",incommingMessageIndex);
switch (bytesReturned) {
case ACK:
- printf("Recv: %x (ACK)",bytesReturned);
+ printf("Recv: %x (ACK)\n",bytesReturned);
+ break;
+ case DLE:
+ printf("Recv: %x (DLE)\n",bytesReturned);
+ break;
+ case EOT:
+ printf("Recv: %x (EOT)\n",bytesReturned);
+ break;
+ case ETX:
+ printf("Recv: %x (ETX)\n",bytesReturned);
break;
case ENQ:
- printf("Recv: %x (ENQ)",bytesReturned);
+ printf("Recv: %x (ENQ)\n",bytesReturned);
break;
case NAK:
- printf("Recv: %x (NAK)",bytesReturned);
+ printf("Recv: %x (NAK)\n",bytesReturned);
break;
default:
- printf("Recv: %x",bytesReturned);
+ printf("Recv: %x\n",bytesReturned);
break;
- }
- cout<<bytesReturned<<endl;
- if(bytesReturned == 0x05){
- serial.writeByte(ACK);
- }
- };
+ }
+
+ //This is where the parsing is happening
+ parseIncommingByte(bytesReturned);
+
+ }
+ if(messageQueue.size() > 0 && !receivingMessage){
+ //There are messages to be send
+ if(!sendingMessage){
+ //ENQ not send yet, lets send it
+ serial.writeByte(ENQ);
+ sendingMessage = true;
+ outgoingFormalities[0] = true;
+ } else if(outgoingFormalities[1]){
+ ADLPMessage msg = messageQueue[0];
+ messageQueue.erase(messageQueue.begin());
+
+ printf("Send message %x\n",msg.instruction);
+
+ unsigned char message[msg.size];
+ memset(message, 0, msg.size);
+
+ message[1] = msg.size; //Size of text message
+ message[2] = 1; //Mesesage from PC to controller
+ message[3] = 0; //Mesesage from PC to controller
+ message[4] = msg.instruction; // Instruction
+ message[5] = msg.messageType;
+ for(int i=0;i<msg.size-7;i++){
+ message[7+i] = msg.data[i];
+ }
+
+ unsigned char checksum = message[0];
+ for(int i=1;i<msg.size;i++){
+ checksum = checksum ^ message[i];
+ }
+ // message[7] = 1;
+
+
+ serial.writeByte(DLE);
+ serial.writeByte((msg.size % 2)? STXeven : STXodd);
+ serial.writeBytes(message, msg.size);
+ serial.writeByte(DLE);
+ serial.writeByte(ETX);
+ serial.writeByte(checksum ^ ETX);
+ }
+
+
+
+ }
+}
+
+void ADLPCom::sendMessage(ADLPInstruction instruction, unsigned char * data, int datasize){
+ ADLPMessage msg;
+ msg.size = datasize + 7;
+ msg.fromComputer = true;
+ msg.instruction = instruction;
+ msg.messageType = query;
+ msg.data = data;
+
+ sendMessage(msg);
+}
+
+void ADLPCom::sendMessage(ADLPMessage msg){
+ messageQueue.push_back(msg);
}
void ADLPCom::sendMessage(){
- unsigned char message[6];
- memset(message, 0, 6);
-
- message[1] = 6; //Size of text message
- message[2] = 1; //Mesesage from PC to controller
- message[3] = 0; //Mesesage from PC to controller
- message[4] = 0x2D; // Instruction
- message[5] = 1;
- // message[7] = 1;
-
-
- serial.writeByte(ENQ);
- serial.writeByte(DLE);
- serial.writeByte(STXodd);
- serial.writeBytes(message, 6);
- serial.writeByte(DLE);
- serial.writeByte(ETX);
- serial.writeByte(message[4] ^ ETX);
+
+}
+
+void ADLPCom::parseIncommingByte(unsigned char bytesReturned){
+ if(bytesReturned == ACK && sendingMessage){
+ if(outgoingFormalities[0] && outgoingFormalities[1]){
+ //Sending was succesfull
+ outgoingFormalities[0] = false;
+ outgoingFormalities[1] = false;
+ sendingMessage = false;
+ } else if(outgoingFormalities[0] && !outgoingFormalities[1]) {
+ //Start sending message
+ outgoingFormalities[1] = true;
+ }
+ }
+ if(bytesReturned == ENQ){
+ //New message is incomming
+ //Return with an acknowledge
+ serial.writeByte(ACK);
+ receivingMessage = true;
+ incommingMessageIndex = 0; //Reset the index counter
+ memset(incommingFormalities,false,5); //reset the formalities handler
+ incommingFormalities[0] = true; //Tell that the first formality is met
+
+ } else if(incommingMessageIndex >= 0 && incommingFormalities[0] && incommingFormalities[1] && incommingFormalities[2] && !incommingFormalities[3] && !incommingFormalities[4] && (incommingMessageIndex <= 1 || incommingMessageIndex < incommingMessageTemp.size) ){
+ //We are processing a message content that is incomming
+ switch (incommingMessageIndex) {
+ case 0:
+ //Not used
+ break;
+ case 1:
+ cout<<"Start new message with size "<<(int)bytesReturned<<endl;
+ incommingMessageTemp.size = bytesReturned;
+ incommingMessageTemp.data = (unsigned char*) malloc(incommingMessageTemp.size*sizeof(unsigned char));
+ break;
+ case 2:
+ if(bytesReturned == 1){
+ incommingMessageTemp.fromComputer = true; //How should this be possible?!
+ } else {
+ incommingMessageTemp.fromComputer = false;
+ }
+ break;
+ case 3:
+ break;
+ case 4:
+ incommingMessageTemp.instruction = (ADLPInstruction)bytesReturned;
+ break;
+ case 5:
+ incommingMessageTemp.messageType = (ADLPMessageType)bytesReturned;
+ if(incommingMessageTemp.messageType == warning){
+ cout<<"WARNING"<<endl;
+ }
+ break;
+ case 6:
+ //Not used
+ break;
+
+ default:
+ //Everything else is data
+ printf("\t\t\t\tData: %x %i\n",bytesReturned,bytesReturned);
+ if(bytesReturned > 31 && bytesReturned < 127)
+ printf("%c",bytesReturned);
+ incommingMessageTemp.data[incommingMessageIndex-7] = bytesReturned;
+ break;
+ }
+
+ incommingMessageIndex++;
+ } else if(bytesReturned == DLE){
+ if(!incommingFormalities[1]){
+ incommingFormalities[1] = true;
+ cout<<"First DLE"<<endl;
+ }else {
+ incommingFormalities[3] = true; //Second time DLE is received after message
+ cout<<"Second DLE"<<endl;
+ }
+ } else if(bytesReturned == STXodd || bytesReturned == STXeven){
+ incommingFormalities[2] = true;
+ cout<<"Got stx"<<endl;
+ } else if(bytesReturned == ETX){
+ //End of text
+ cout<<"got etx"<<endl;
+ incommingFormalities[4] = true;
+ } else if(bytesReturned == EOT){
+ //End of transmission
+ cout<<"got EOT"<<endl;
+ printf("Done receiving at msg %i\n",incommingMessageIndex);
+ printf("Received instruction: %x",incommingMessageTemp.instruction );
+ if(incommingMessageTemp.messageType == warning){
+ cout<<"WARNING"<<endl;
+ }
+ cout<<incommingMessageTemp.data<<endl;
+ incommingMessageIndex = -1;
+ }
+ else if(incommingMessageIndex >= 0 && incommingFormalities[0] && incommingFormalities[1] && incommingFormalities[2] && incommingFormalities[3] && incommingFormalities[4]){
+ printf("Recevied checksum %x\n",bytesReturned);
+ //All formalities met, check cheksum (TODO)
+ serial.writeByte(ACK);
+ receivingMessage = false;
+
+ }
+
}
View
@@ -31,7 +31,24 @@ enum ADLPMessageType {
};
enum ADLPInstruction {
- ARAPVersion = 0x2D
+ uploadProgram = 0x01,
+ startProgram = 0x02,
+ stopProgram = 0x03, //confirmed
+ TCPvalue = 0x04,
+ locationValue = 0x05,
+ registerValue = 0x06,
+ sensorValue = 0x07,
+ configurationValue = 0x0A,
+ frameValue = 0x0B,
+ statusList = 0x13,
+ operationMode = 0x14,
+ programList = 0x15,
+ eraseProgram = 0x16,
+ arcWeldValue = 0x19,
+ downloadProgram = 0x1D,
+ ARAPVersion = 0x2D,
+ resolverValue = 0x2E,
+ automaticStatusUpdate = 0x7F //return from robot at emergency break
};
struct ADLPMessage {
@@ -49,10 +66,22 @@ class ADLPCom{
void update();
void sendMessage();
-
+ void sendMessage(ADLPInstruction instruction, unsigned char * data, int datasize);
+ void sendMessage(ADLPMessage message);
+
bool connected;
-
+ bool receivingMessage;
+ bool sendingMessage;
private:
ofSerialEven serial;
+
+ int incommingMessageIndex;
+ ADLPMessage incommingMessageTemp;
+ bool incommingFormalities[5];
+
+ void parseIncommingByte(unsigned char bytesReturned);
+
+ vector<ADLPMessage> messageQueue;
+ bool outgoingFormalities[2];
};

0 comments on commit 187db54

Please sign in to comment.