lib/move.js
function move (leftPort, rightPort) {
  leftPort = leftPort || 'b'
  rightPort = rightPort || 'c'
  var write = writeAction('motors_write', [leftPort, rightPort])
  /**
   * Run motors forever
   * @param  {Number} speed speed of motor
   * @param  {Object} opts  object of optional params
   */
  function * forever (speed, turn, opts) {
    speed = speed || motorDefaults.speed
    var speeds = turnToSpeeds(turn, speed)

    yield write('run-forever', {
      left: {
        'speed_sp': speeds.left.toString()
      }, right: {
        'speed_sp': speeds.right.toString()
      }
    })
  }

  /**
   * Run drive motors for a number of degrees
   * @param  {Number} degrees degrees to turn motor
   * @param  {Number} speed   speed at which to turn motors
   * @param  {Number} turn    turn direction
   */
  function * degrees (deg, speed, turn) {
    speed = speed || motorDefaults.speed

    var opts = turnToDegrees(turn, speed, deg)
    yield * writeAndWait('run-to-rel-pos', {
      left: {
        'position_sp': opts.left.degrees.toString(),
        'speed_sp': opts.left.speed.toString()
      },
      right: {
        'position_sp': opts.right.degrees.toString(),
        'speed_sp': opts.right.speed.toString()
      }
    })
  }

  /**
   * Run drive motors for a number of rotations
   * @param  {Number} rotations number of rotations to turn the motor
   * @param  {Number} speed     speed at which to turn motors
   * @param  {Number} turn      turn direction
   */
  function * rotations (rots, speed, turn) {
    speed = speed || motorDefaults.speed
    yield * this.degrees(Math.round(rots * 360), speed, turn)
  }

  /**
   * Run drive motors for a specified amount of time
   * @param  {Number} time  time to run the motors for (in milliseconds)
   * @param  {Number} speed speed at which to turn motors
   * @param  {Number} turn  turn direction
   */
  function * timed (time, speed, turn) {
    speed = speed || motorDefaults.speed

    var speeds = turnToSpeeds(turn, speed)
    yield * writeAndWait('run-timed', {
      left: {
        'time_sp': time.toString(),
        'speed_sp': speeds.left.toString()
      },
      right: {
        'time_sp': time.toString(),
        'speed_sp': speeds.right.toString()
      }
    })
  }

  /**
   * Stops motors
   */
  function * stop () {
    yield write('stop', {left: {}, right: {}})
  }

  /**
   * Write and hold execution until the motors are done moving
   * @param  {string} command move command
   * @param  {object} opts    move options
   */
  function * writeAndWait (command, opts) {
    var ran = false
    yield write(command, opts)
    while (true) {
      var devices = yield * read()
      if (devices.motor(leftPort) === 'running' || devices.motor(rightPort) === 'running') {
        ran = true
      }
      if (devices.motor(leftPort) === '' && devices.motor(rightPort) === '' && ran) {
        break
      }
    }
  }

  return {
    forever: forever,
    degrees: degrees,
    rotations: rotations,
    timed: timed,
    stop: stop
  }
}
function move (leftPort, rightPort) {
  leftPort = leftPort || 'b'
  rightPort = rightPort || 'c'
  var write = writeAction('motors_write', [leftPort, rightPort])
  /**
   * Run motors forever
   * @param  {Number} speed speed of motor
   * @param  {Object} opts  object of optional params
   */
  function * forever (speed, turn, opts) {
    speed = speed || motorDefaults.speed
    var speeds = turnToSpeeds(turn, speed)

    yield write('run-forever', {
      left: {
        'speed_sp': speeds.left.toString()
      }, right: {
        'speed_sp': speeds.right.toString()
      }
    })
  }

  /**
   * Run drive motors for a number of degrees
   * @param  {Number} degrees degrees to turn motor
   * @param  {Number} speed   speed at which to turn motors
   * @param  {Number} turn    turn direction
   */
  function * degrees (deg, speed, turn) {
    speed = speed || motorDefaults.speed

    var opts = turnToDegrees(turn, speed, deg)
    yield * writeAndWait('run-to-rel-pos', {
      left: {
        'position_sp': opts.left.degrees.toString(),
        'speed_sp': opts.left.speed.toString()
      },
      right: {
        'position_sp': opts.right.degrees.toString(),
        'speed_sp': opts.right.speed.toString()
      }
    })
  }

  /**
   * Run drive motors for a number of rotations
   * @param  {Number} rotations number of rotations to turn the motor
   * @param  {Number} speed     speed at which to turn motors
   * @param  {Number} turn      turn direction
   */
  function * rotations (rots, speed, turn) {
    speed = speed || motorDefaults.speed
    yield * this.degrees(Math.round(rots * 360), speed, turn)
  }

  /**
   * Run drive motors for a specified amount of time
   * @param  {Number} time  time to run the motors for (in milliseconds)
   * @param  {Number} speed speed at which to turn motors
   * @param  {Number} turn  turn direction
   */
  function * timed (time, speed, turn) {
    speed = speed || motorDefaults.speed

    var speeds = turnToSpeeds(turn, speed)
    yield * writeAndWait('run-timed', {
      left: {
        'time_sp': time.toString(),
        'speed_sp': speeds.left.toString()
      },
      right: {
        'time_sp': time.toString(),
        'speed_sp': speeds.right.toString()
      }
    })
  }

  /**
   * Stops motors
   */
  function * stop () {
    yield write('stop', {left: {}, right: {}})
  }

  /**
   * Write and hold execution until the motors are done moving
   * @param  {string} command move command
   * @param  {object} opts    move options
   */
  function * writeAndWait (command, opts) {
    var ran = false
    yield write(command, opts)
    while (true) {
      var devices = yield * read()
      if (devices.motor(leftPort) === 'running' || devices.motor(rightPort) === 'running') {
        ran = true
      }
      if (devices.motor(leftPort) === '' && devices.motor(rightPort) === '' && ran) {
        break
      }
    }
  }

  return {
    forever: forever,
    degrees: degrees,
    rotations: rotations,
    timed: timed,
    stop: stop
  }
}
function move ( leftPort , rightPort ) { leftPort = leftPort || 'b' rightPort = rightPort || 'c' var write = writeAction ( 'motors_write' , [ leftPort , rightPort ] ) /**
   * Run motors forever
   * @param  {Number} speed speed of motor
   * @param  {Object} opts  object of optional params
   */ function * forever ( speed , turn , opts ) { speed = speed || motorDefaults . speed var speeds = turnToSpeeds ( turn , speed ) yield write ( 'run-forever' , { left : { 'speed_sp' : speeds . left . toString ( ) } , right : { 'speed_sp' : speeds . right . toString ( ) } } ) } /**
   * Run drive motors for a number of degrees
   * @param  {Number} degrees degrees to turn motor
   * @param  {Number} speed   speed at which to turn motors
   * @param  {Number} turn    turn direction
   */ function * degrees ( deg , speed , turn ) { speed = speed || motorDefaults . speed var opts = turnToDegrees ( turn , speed , deg ) yield * writeAndWait ( 'run-to-rel-pos' , { left : { 'position_sp' : opts . left . degrees . toString ( ) , 'speed_sp' : opts . left . speed . toString ( ) } , right : { 'position_sp' : opts . right . degrees . toString ( ) , 'speed_sp' : opts . right . speed . toString ( ) } } ) } /**
   * Run drive motors for a number of rotations
   * @param  {Number} rotations number of rotations to turn the motor
   * @param  {Number} speed     speed at which to turn motors
   * @param  {Number} turn      turn direction
   */ function * rotations ( rots , speed , turn ) { speed = speed || motorDefaults . speed yield * this . degrees ( Math . round ( rots * 360 ) , speed , turn ) } /**
   * Run drive motors for a specified amount of time
   * @param  {Number} time  time to run the motors for (in milliseconds)
   * @param  {Number} speed speed at which to turn motors
   * @param  {Number} turn  turn direction
   */ function * timed ( time , speed , turn ) { speed = speed || motorDefaults . speed var speeds = turnToSpeeds ( turn , speed ) yield * writeAndWait ( 'run-timed' , { left : { 'time_sp' : time . toString ( ) , 'speed_sp' : speeds . left . toString ( ) } , right : { 'time_sp' : time . toString ( ) , 'speed_sp' : speeds . right . toString ( ) } } ) } /**
   * Stops motors
   */ function * stop ( ) { yield write ( 'stop' , { left : { } , right : { } } ) } /**
   * Write and hold execution until the motors are done moving
   * @param  {string} command move command
   * @param  {object} opts    move options
   */ function * writeAndWait ( command , opts ) { var ran = false yield write ( command , opts ) while ( true ) { var devices = yield * read ( ) if ( devices . motor ( leftPort ) === 'running' || devices . motor ( rightPort ) === 'running' ) { ran = true } if ( devices . motor ( leftPort ) === '' && devices . motor ( rightPort ) === '' && ran ) { break } } } return { forever : forever , degrees : degrees , rotations : rotations , timed : timed , stop : stop } }
Set up drive motors and returns functions to control them.
@param  {string} leftPort  port that the left motor is connected to
@param  {string} rightPort port that the right motor is connected to
@return {object}           steering functions
Set up drive motors and returns functions to control them .