Help debugging

Hey Chris,

We are encountering somewhat rare, but critical, bugs in our Mworks code.

Background:
We use pin 12 on an Arduino device connected via Firmata bluetooth to drive a fluid delivery circuit. When it is high, the circuit runs a DC current through a pump that delivers fluid continuously. When it is low, no current flows through the pump, and it is inactive.

The bug:
While the task is running, every once in a while, the pump dispenses water continuously for an extended length of time. It then stops at some point.

Observations:

  • The bug has been observed to start while the subject is doing trials.
  • Stopping the experiment and performing a reload of the .mwel immediately ceases the bug.
  • The bug has occurred across 3 different circuits in 3 different rigs.
  • So, it seems unlikely it is due to a hardware bug.

My speculations on source of bug:
I think this has to do with the Firmata interface.

We request that pin 12 is set to high through a variable assignment (see code below). My guess is that the first assignment (pump_control_line = true) succeeds, but the second assignment and third assignment both fail.

The monkey then stops working, as the water is flowing continuously. If the monkey happens to do another trial, one of the pump_control_line = false assignments eventually succeeds, and correct code behavior resumes. My guess is this bug could happen more frequently than we have observed because of this.

This seems related to another behavior we have previously observed (and I have brought up with you earlier at some point, I think), which is that setting cur_reward_duration_ms to values around ~25 msec causes the juicer to sporadically fail to dispense juice, which can be explained by a failure of setting pin12 to high pump_control_line = true (as it is overrided by pump_control_line = false).

In our case, it’s really important that we can verify that the juicer circuit is engaged as requested. One proposal is to rewrite the Arduino interface + the firmware running on the Arduino itself, so we have the ability to send encoded pin commands (e.g., set pin 12 to high for 50 ms, then turn off), instead of trying to encode pin commands on the Mworks side. Even better to also have mworks be able to receive messages from the Arduino and verify that a pin request has succeeded before resuming mworks code.

Any workarounds would be appreciated too.

Thanks,
Michael

‘’’

firmata pump_output_device (
bluetooth_local_name = bluetooth_local_name
autostart = YES
//reconnect_interval = 5s // Not supported in current mworks version?
//connected = pump_connected
){
firmata_digital_output (
pin_number = 12
value = pump_control_line
)
}

var reward = 0 {
if (reward > 0){
play_sound (reward_sound)
pump_control_line = true
wait (
duration = cur_reward_duration_ms
duration_units = ms
)
pump_control_line = false
total_msec_pump_on = total_msec_pump_on + reward_duration_ms
report(‘Reward! ( $reward_duration_ms msec )’)
}

pump_control_line = false
}
‘‘’

Hi Michael,

I’m surprised that the message to set the pump control line low is being lost, but I agree that it’s the most likely explanation of the problem. I also agree that the on/off timing should be handled on the device, not in MWorks. This would also let you avoid the timing issues associated with Bluetooth LE.

Let me think about the best way to handle this. Be aware that, whatever solution we devise, you will definitely need to install new firmware on the Arduino boards, so you’ll need to be able to get them out of their boxes (or at least expose them enough to connect a USB cable).

Cheers,
Chris

Thanks Chris. Not a problem, it’s easy to access the Arduino boards.

Hi Michael,

Just a quick update:

I’ve worked out the changes needed in the Firmata protocol, and I’m now implementing them in both the Firmata firmware and MWorks. I’ll let you know as soon as they’re ready for testing.

Cheers,
Chris

Great. Thank you!

Hi Michael,

I’ve finished implementing my changes in the Firmata firmware and MWorks.

MWorks now supports two new channel types for Firmata devices. The one you’ll be most interested in is the digital output pulse channel. This looks a lot like a regular digital output channel. The difference is that you assign durations to the value parameter. Every time you make an assignment, MWorks sends a pulse request to the Firmata device. The device then sets the channel’s associated pin high, leaves it on for the specified duration, then resets it to low. By using a pulse output channel, you can both (1) eliminate the need to manually set the pin back to low via MWorks and (2) avoid the timing limitations associated with the BLE connection.

The second new channel type is the digital input pulse channel. As you can probably guess, this channel type watches for pulses on an input pin (i.e the pin going from low to high and back to low) and reports the duration of each pulse. As with output pulse channels, the pulse timing is measured entirely on the Firmata device, and MWorks is notified by a single message when the pulse completes. I added this channel type mostly for symmetry and to aid my own testing. However, it could be useful to you as a means of confirming that juice has been delivered. Specifically, I’m thinking you could connect the juice control output pin to both the juice pump and an input pin on the Firmata board. If you then configure that pin as a pulse input channel, you’ll receive measured “juice on” durations in MWorks. If your experiment waits for these, then it can confirm that the pump was on for the expected duration.

To make use of these new channel types, you’ll need to install the new Firmata library with MWorks extensions on your boards. I’ve updated the setup instructions to use this library. (Note that it strictly adds features to the standard Firmata library, so you’re free to install it on your boards and use it with older MWorks versions that don’t support the new channel types.)

You’ll also need an updated MWorks version. To try things out on a Mac, you can use the current nighty build. To get it on your iPad’s, you have a couple options. First, I’m hoping to release a new, “official” version of MWorks in the next couple weeks (though I can’t make any guarantees on the timing). If you can wait for that, then you can just update MWorks from the App Store once the release is out.

Alternatively, if you want to use the new Firmata features ASAP, I can create an ad hoc distribution for you. This isn’t hard, but it requires me to register the device ID for every iPad on which MWorks will be installed. You would need to provide me with the device ID’s beforehand. Once I create the ad hoc build, I’d share the IPA file with you via Dropbox. You’d then have to install it using Xcode or Apple Configurator 2, both of which are available from the Mac App Store.

Please let me know how you’d like to proceed.

Cheers,
Chris

Hi Chris,

Thank you, the changes look like they’ll resolve the issue nicely.

I will wait until the App Store publishes the latest iOS build, and will test this out on my setup then.

Michael

Hi Michael,

The new version of MWorks is now available.

Cheers,
Chris