Understanding events in Johnny-Five

The events in Johnny-Five are a very important concept—this is also a new concept, especially if you are used to low-level language programming. It's very similar to the idea of interrupts, but definitely strays away from the traditional robotics programming paradigm of an event loop. While you can create timers and loops in Johnny-Five, it highly encourages an event-based programming approach, which can need some practice.

Why events?

A question that gets asked a lot is, "Why event-based? Why not loop-based and interrupt-based as in previous methods?".

A lot of this has to do with the way robots work and the way we think about how we program robots. When you think about what you want a robot to do, you're less likely to think "Every X seconds, I want to check for A and start task B..." and you are more likely to think "When Y happens, I want to start event C...".

The event-based system in Johnny-Five works really well with this train of thought by allowing users to place listeners and handlers in the events instead of polling for the correct conditions every X seconds. It makes robotics programming a little easier to grasp for those who are new to robotics programming.

Grasping events is very important to understanding Johnny-Five—this is because every Johnny-Five script begins by instantiating a Board object and waiting for it to fire a ready event. This is the equivalent of the DOM ready event in browser-based JavaScript applications—it tells you that you're ready to start sending instructions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.144.32