Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device.send() doesn't work in ofApp::setup() - PacketSerial #17

Open
vvzen opened this issue Aug 27, 2018 · 3 comments
Open

device.send() doesn't work in ofApp::setup() - PacketSerial #17

vvzen opened this issue Aug 27, 2018 · 3 comments

Comments

@vvzen
Copy link

vvzen commented Aug 27, 2018

Hi,
I'm using this addon for an installation where I'm controlling multiple steppers and servos and it works great if the code where I send the message via serial is inside an event listener function (like mousePressed() or keyPressed() ), but not if I use it inside setup() or update().

To be more precise, this is the code that I'm using to send the osc message via serial (modelled on the OSC-SLIP packet example):

ofxOscMessage osc_message;
osc_message.setAddress("/home");
osc_message.addIntArg(1);

send_osc_bundle(osc_message, cnc_device, 1024);
  1. If I only put these 4 lines inside a ofApp::keyPressed(), it works ok. ✅

  2. If I instead put them inside ofApp::setup() , after setting up the serial device, it does not work. ❌

  3. Finally, if I put them inside ofApp::setup() and also inside ofApp::keyPressed(), even the keyPressed() event doesn't work. ❌

For reference, this is my send_osc_bundle().

//--------------------------------------------------------------
void ofApp::send_osc_bundle(ofxOscMessage &m, ofxIO::SLIPPacketSerialDevice &device, int buffer_size){
    
    // this code comes from ofxOscSender::sendMessage in ofxOscSender.cpp
    // static const int OUTPUT_BUFFER_SIZE = buffer_size;
    char buffer[buffer_size];

    // create the packet stream
    osc::OutboundPacketStream p(buffer, buffer_size);
    
    p << osc::BeginBundleImmediate; // start the bundle
    append_message(m, p);           // add the osc message to the bundle
    p << osc::EndBundle;            // end the bundle
 
    // send to device
    device.send(ofxIO::ByteBuffer(p.Data(), p.Size()));
}

The append_message() is the exact same code of the examples.

Any hint would be great! I've been struggling with this for quite a while.

Thanks!

@vvzen
Copy link
Author

vvzen commented Aug 27, 2018

As an addendum, it might just be that I need to wait a little bit before sending data?

If I do something like that, it seems to be working good.
(homed starts as false)

void ofApp::update(){

    if ((int) ofGetElapsedTimef() < 6){
        int elapsed_time = (int) ofGetElapsedTimef();
        ofLogNotice() << "elapsed time: " << elapsed_time;

        if (elapsed_time == 5 && !homed){
            ofLogNotice() << "sending home";
       
            ofxOscMessage osc_message;
            osc_message.setAddress("/home");
            osc_message.addIntArg(1);

            send_osc_bundle(osc_message, cnc_device, 1024);

            homed = true;
        }
    }
}

@bakercp
Copy link
Owner

bakercp commented Aug 27, 2018

It's hard to say what's going on without knowing the exact hardware setup / Arduino code. As far as I know you should be able to send serial commands in setup(). Rather than timers etc, I would make the Arduino respond with an ACK like message of some kind before proceeding ... ?

@vvzen
Copy link
Author

vvzen commented Aug 28, 2018

@bakercp Thanks for answering!
I'm using an Arduino Mega, the code can be found here: https://github.com/vvzen/maca-final/blob/master/arduino/stepper-no-lib-mega/stepper-no-lib-mega.ino
Yep probably in the final build I will do a handshake or acknowledgement sort of thing before starting sending to the arduino.
But for now waiting (even a second) before sending the first message seems to work. ( https://github.com/vvzen/maca-final/blob/master/coherent-lines-dots/src/ofApp.cpp )
Maybe there's a race condition somewhere?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants