rwaldron / johnny-five

JavaScript Robotics and IoT programming framework, developed at Bocoup.
http://johnny-five.io
Other
13.3k stars 1.77k forks source link

Enforce Pin PWM-ness where necessary #129

Closed rwaldron closed 11 years ago

rwaldron commented 11 years ago

It would be useful to display a warning in the REPL that makes a user aware that they've made a mistake in these (and other) cases:

  1. Code that attempts to initialize a servo on a non-PWM pin
  2. Code that attempts PWM-specific operations on components that otherwise function on non-PWM pins (eg. setting brightness on an Led)

others?

reconbot commented 11 years ago

This is a great idea, I came across this a bunch during nodebots. Would you want to throw an Error?

divanvisagie commented 11 years ago

While we are on the topic of PWM pins I also noticed that the code for checking PWM is pretty much hard coded to the UNO and incompatible with the Mega which has different PWM pins. Thinking maybe a type option in Board(). Working on it here but it's currently untested

rwaldron commented 11 years ago

@divanvisagie oof, yeah, good call.

Need:

  1. Per board PWM, Digital, Analog, I2C pin "layouts"
  2. Decide whether to throw or just log an error

Thoughts?

rwaldron commented 11 years ago

Firmata.h, Firmata.cpp and Boards.h all know what kind of board is attached—I wish we could do this in a way that didn't require the user to explicitly specify anything

reconbot commented 11 years ago

Firmata reports a lot when you ask =) http://firmata.org/wiki/Protocol#Capability_Query

rwaldron commented 11 years ago

I wasn't able to pursue this earlier—I'm in Boston without a board :(

EDIT: This looks like exactly what we need for the task

divanvisagie commented 11 years ago

Im sure the above mentioned commit caused this issue so im putting it here instead of creating a new one.

When running the following code on an UNO everything works fine:

   var five = require( 'johnny-five' ),
    board;

board = new five.Board();

var led;
board.on( 'ready', function(){

    led = new five.Led(13);

    this.loop( 500, function(){
        led.toggle();
    });

} );

Things dont turn out too well however on my Spider:

/Users/divanvisagie/node_modules/johnny-five/lib/board.js:740
  return Object.keys( translations ).reduce(function( pin, map ) {
                ^
TypeError: Object.keys called on non-object
    at Function.keys (native)
    at Function.Board.Pins.translate (/Users/divanvisagie/node_modules/johnny-five/lib/board.js:740:17)
    at Function.Board.Pins.normalize (/Users/divanvisagie/node_modules/johnny-five/lib/board.js:654:20)
    at Led.Board.Device (/Users/divanvisagie/node_modules/johnny-five/lib/board.js:554:21)
    at new Led (/Users/divanvisagie/node_modules/johnny-five/lib/led.js:28:16)
    at Board.<anonymous> (/Users/divanvisagie/Desktop/led.js:11:8)
    at Board.EventEmitter.emit (events.js:95:17)
    at Board.<anonymous> (/Users/divanvisagie/node_modules/johnny-five/lib/board.js:288:14)
    at Board.<anonymous> (/Users/divanvisagie/node_modules/johnny-five/lib/board.js:130:18)
    at null.<anonymous> (/Users/divanvisagie/node_modules/firmata/lib/firmata.js:330:25)

Will update any progress on this.

EDIT:

So far the problem looks like its here:

https://github.com/rwldrn/johnny-five/blob/master/lib/board.js#L739

Using the uno the value of translations is:

 { tinker: 
    { I0: 'A0',
      I1: 'A1',
      I2: 'A2',
      I3: 'A3',
      I4: 'A4',
      I5: 'A5',
      O0: 11,
      O1: 10,
      O2: 9,
      O3: 6,
      O4: 5,
      O5: 3,
      D13: 13,
      D12: 12,
      D8: 8,
      D7: 7,
      D4: 4,
      D2: 2 } }

However on the spider translations is undefined.

divanvisagie commented 11 years ago

Looks like the problem is quite simple: The MEGA isn't mapped yet

 var totalPins = {
   20: {
     UNO: [ 0, 0, 3, 4, 3, 4, 4, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3 ]
   },
   70: {
     MEGA: [ ]
   }
 };

EDIT:

https://github.com/rwldrn/johnny-five/pull/147

rwaldron commented 11 years ago

This was part of the issue, the other is that I needed to handle cases where no translations exist. I fixed that yesterday and pushed a new version to npm.

Still need analog mapping for mega!

divanvisagie commented 11 years ago

@rwldrn wasn't that what this was for? https://gist.github.com/rwldrn/5792793

rwaldron commented 11 years ago

Huh—Yes. I never received any notifications about comments on the gist. shrugs

brainrake commented 11 years ago

I was trying to get the Leonardo working, and found it is missing pin mappings. But I'm not sure this is the best approach. For one, there aren't different boards with the same number of pins, so this is unnecessary abstraction. Furthermore, the matching is done using only the number of modes a pin supports, which might be inaccurate. How about the following:

var totalPins = {
  20: 'UNO',
  30: 'LEONARDO',
  70: 'MEGA'
};

and:

type = totalPins[length];
brainrake commented 11 years ago

Plus a case should be added for when there's no match. Is a pull request more appropriate?

rwaldron commented 11 years ago

This was implemented, but has since been scaled back to a debug mode opt-in