Skip to content
This repository

Add Rover constructor #63

Closed
wants to merge 1 commit into from

3 participants

Andreas Haugstrup Pedersen Rick Waldron Divan Visagie
Andreas Haugstrup Pedersen

First attempt at a rover constructor. As you can see I based it very heavily on the navigator example, but I did decide to make some decisions I'm unsure of how well will go over :)

The aliases for movement methods (e.g. fwd and rev) are gone because I felt they cluttered the code without providing a ton of benefit. At the same time I went away with the dynamically generated methods for two reasons:

  • It makes the code much easier to comprehend on the first read through (very important to me)
  • I could get rid of the directionMap object literal which made it hard for me to read and understand the code

Instead there are two methods go (forward, reverse movement) and pivot (left right pivoting) which handle movement and four convenience methods (forward, reverse, left, right) which just calls either go or pivot.

In the same go I'm using pivot for what the Navigator example called turning because I feel moving tracks in opposite directions are more appropriately called pivoting while turning only one track at a time should be called turning. I haven't implemented what the Navigator example called pivoting yet.

Rick Waldron
Owner

I very specifically made the aliases to reduce typing when controlling from REPL. I'm headed to New York tomorrow so I won't be able to try this out until Monday

Andreas Haugstrup Pedersen

That makes sense. And no worries, I figure this will take some rounds to get right anyway.

Rick Waldron
Owner

Cool, glad you're up for smoothing it all out. Looking forward to digging into this on Monday :)

Rick Waldron
Owner

I promise I haven't forgotten about this, nor am I ignoring it ;)

I've been moving from Boston to Brooklyn since last week and all of my stuff is still at Bocoup

Andreas Haugstrup Pedersen

I know, no worries. I'm almost done building something else while I wait for your comments. Real life got in the way of robots for me also.

Rick Waldron
Owner

Finally have everything unpacked so I'll be able to dig in soon!!

Andreas Haugstrup Pedersen

Sounds great. I'm been burried in work the past month. Had my arduino out last night for the first time in forever (to see if I could run the 7-segment example) so I'm also ready to make any changes you want. :)

Divan Visagie
Collaborator

when you say rover do you mean this? http://www.pololu.com/catalog/product/1551/specs

Rick Waldron
Owner

@divanvisagie I think this was meant for a two-wheeled bot that was driven with opposing continuous servos.

Divan Visagie
Collaborator

@rwaldron This PR is a year old now with no resolve. I think it would be nice to get it off the issue list. Feelings?

Rick Waldron
Owner

Let's close for now

@haugstrup you should publish this a module on npm :)

Rick Waldron rwaldron closed this December 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
33  eg/rover.js
... ...
@@ -0,0 +1,33 @@
  1
+var five = require("../lib/johnny-five.js");
  2
+
  3
+board = new five.Board();
  4
+
  5
+board.on("ready", function() {
  6
+
  7
+  rover = new five.Rover({
  8
+    right: 10,
  9
+    left: 11
  10
+  });
  11
+
  12
+  this.repl.inject({
  13
+    rover: rover
  14
+  });
  15
+
  16
+  rover.on("ready", function() {
  17
+
  18
+    console.log('rover is ready...');
  19
+
  20
+    // Move forward for 2 seconds
  21
+    // Pivot right for 1 second
  22
+    rover.forward();
  23
+    setTimeout(function(){
  24
+      rover.right(1000);
  25
+    }, 2000);
  26
+
  27
+    // Stop after 5 total seconds
  28
+    setTimeout(function(){
  29
+      rover.stop();
  30
+    }, 5000);
  31
+
  32
+  });
  33
+});
1  lib/johnny-five.js
@@ -23,6 +23,7 @@
23 23
   "Pir",
24 24
   // "PWMServo",
25 25
   "Repl",
  26
+  "Rover",
26 27
   "Sensor",
27 28
   "Servo",
28 29
   "ShiftRegister",
193  lib/rover.js
... ...
@@ -0,0 +1,193 @@
  1
+// Control 2-servo rover. Based heavily on
  2
+// https://github.com/rwldrn/johnny-five/blob/master/eg/navigator.js
  3
+var Board = require("../lib/board.js"),
  4
+    events = require("events"),
  5
+    util = require("util"),
  6
+    Servo = require("../lib/servo.js"),
  7
+    __ = require("../lib/fn.js");
  8
+
  9
+scale = function( speed, low, high ) {
  10
+  return Math.floor( __.map( speed, 0, 5, low, high ) );
  11
+};
  12
+
  13
+/**
  14
+ * Rover
  15
+ * @constructor
  16
+ *
  17
+ * @param {Object} opts Options: right, left, center
  18
+ */
  19
+function Rover( opts ) {
  20
+  if ( !(this instanceof Rover) ) {
  21
+    return new Rover( opts );
  22
+  }
  23
+
  24
+  opts = Board.options( opts );
  25
+
  26
+  // Default center calibration at 90°
  27
+  this.center = opts.center || 90;
  28
+
  29
+  // Initialize the right and left cooperative servos
  30
+  this.servos = {
  31
+    right: new Servo({ pin: opts.right, type: "continuous" }),
  32
+    left: new Servo({ pin: opts.left, type: "continuous" })
  33
+  };
  34
+
  35
+  // Set the initial servo cooperative direction
  36
+  this.direction = {
  37
+    right: this.center,
  38
+    left: this.center
  39
+  };
  40
+
  41
+  // Store the cooperative speed. Int. 0-5
  42
+  this.speed = 0;
  43
+
  44
+  // Initial direction
  45
+  this.which = "stop";
  46
+
  47
+  // Store a recallable history of movement
  48
+  // TODO: Include in savable history
  49
+  this.history = [];
  50
+
  51
+  // Wait 10ms, send forward pulse on, then off to
  52
+  // "wake up" the servos
  53
+  setTimeout(function() {
  54
+    this.forward(1).stop(0);
  55
+
  56
+    this.emit.call( this, "ready", null );
  57
+  }.bind(this), 10);
  58
+}
  59
+
  60
+util.inherits( Rover, events.EventEmitter );
  61
+
  62
+/**
  63
+ * move Move the bot in an arbitrary direction
  64
+ * @param  {Number} right Speed/Direction of right servo
  65
+ * @param  {Number} left  Speed/Direction of left servo
  66
+ * @return {Object} this
  67
+ */
  68
+Rover.prototype.move = function( right, left ) {
  69
+
  70
+  // Quietly ignore duplicate instructions
  71
+  if ( this.direction.right === right &&
  72
+        this.direction.left === left ) {
  73
+    return this;
  74
+  }
  75
+
  76
+  // Cooperative servo motion.
  77
+  // Servos are mounted opposite of each other,
  78
+  // the values for left and right will be in
  79
+  // opposing directions.
  80
+  this.servos.right.move( right );
  81
+  this.servos.left.move( left );
  82
+
  83
+  // Push a record object into the history
  84
+  this.history.push({
  85
+    timestamp: Date.now(),
  86
+    right: right,
  87
+    left: left
  88
+  });
  89
+
  90
+  // Update the stored direction state
  91
+  this.direction.right = right;
  92
+  this.direction.left = left;
  93
+
  94
+  return this;
  95
+};
  96
+
  97
+/**
  98
+ * stop Stops the rover, regardless of current direction
  99
+ * @return {Object} this
  100
+ */
  101
+Rover.prototype.stop = function() {
  102
+  this.speed = 0;
  103
+  this.which = "stop";
  104
+
  105
+  return this.move( this.center, this.center );
  106
+};
  107
+
  108
+/**
  109
+ * go Makes rover go, forward or reverse
  110
+ * @return {Object} this
  111
+ */
  112
+Rover.prototype.go = function( direction, speed ) {
  113
+  this.speed = speed || 1;
  114
+  this.which = direction;
  115
+  var args = function( direction, center, val ) {
  116
+    if ( direction === "forward" ) {
  117
+      return [ center - (val - center), val ];
  118
+    }
  119
+    else if ( direction === "reverse" ) {
  120
+      return [ val, center - (val - center) ];
  121
+    }
  122
+    return [ center, center ];
  123
+  };
  124
+
  125
+  // TODO: Should 110 not be the high point of the servo's range?
  126
+  return this.move.apply( this, args( direction, this.center, scale( this.speed, this.center, 110 ) ) );
  127
+};
  128
+
  129
+/**
  130
+ * pivot Pivots the rover around its own axis
  131
+ * @return {Object} this
  132
+ */
  133
+Rover.prototype.pivot = function( direction, duration, speed ) {
  134
+  // Scale the current speed to a servo speed
  135
+  speed = speed || this.speed || 1;
  136
+  var scaled = scale( speed, this.center, 110 );
  137
+  if ( direction === "left" ) {
  138
+    scaled = this.center - (scaled - this.center);
  139
+  }
  140
+
  141
+  // Set both servos to same number to pivot
  142
+  this.move(scaled, scaled);
  143
+
  144
+  // Resume forward or reverse movement after turn has finished
  145
+  // Only if duration has been set
  146
+  if ( duration ) {
  147
+    setTimeout(function(){
  148
+      this[ this.which ](this.speed);
  149
+    }.bind(this), duration);
  150
+  }
  151
+
  152
+  return this;
  153
+};
  154
+
  155
+/**
  156
+ * forward Moves rover forward at given speed
  157
+ * @return {Object} this
  158
+ */
  159
+Rover.prototype.forward = function( speed ) {
  160
+  return this.go( "forward", speed );
  161
+};
  162
+
  163
+/**
  164
+ * reverse Moves rover in reverse at given speed
  165
+ * @return {Object} this
  166
+ */
  167
+Rover.prototype.reverse = function( speed ) {
  168
+  return this.go( "reverse", speed );
  169
+};
  170
+
  171
+/**
  172
+ * right Pivots rover right around own axis
  173
+ * @return {Object} this
  174
+ */
  175
+Rover.prototype.right = function( duration, speed ) {
  176
+  return this.pivot( "right", duration, speed );
  177
+};
  178
+
  179
+/**
  180
+ * left Pivots rover left around own axis
  181
+ * @return {Object} this
  182
+ */
  183
+Rover.prototype.left = function( duration, speed ) {
  184
+  return this.pivot( "left", duration, speed );
  185
+};
  186
+
  187
+// TODO: left/right are pivoting functions.
  188
+// Would be useful to also have the ability to turn while
  189
+// maintaining forward/reverse motion while turning
  190
+// by speeding up or slowing down one of the tracks
  191
+
  192
+module.exports = Rover;
  193
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.