javascript / bonescript structure

i am coming from a background of PIC mcu and arduino. i have a BBB and a Raspberry PI. i prefer the BBB because of the amount of gpios. i have some coding experience using C and the arduino programming language. so i am used to using that structure.

headers and include files

declare Variables

do you set up of ports, pin modes, and/or peripherals.

main loop

user defined functions

My question is what is the structure of a program when using javascript/bonescript to program the BBB. I have read and watched videos of derek molloy that i can just write C or C++ code on the BBB, which requires me to understand the file system and the exporting of the gpios and all that. i am not ready to do that just yet. i have also seen that python is a good option for both the BBB and the raspberry pi, but i want to try the bonescript/javascript first and try writing some code to control a robot. right now i am working out some code to check the number of rotations using disk wheel encoders, so that i can calibrate my motors. i have included the code i am trying to work out. so far this works to count the rotations and output the information to the console, but from here i get stuck.

if i was doing this on a mcu:

  1. i would create variable and configure my interrupt on change pins
  2. write the isr’s to check and do the math, when an interrupt occured
  3. configure a timer interrupt to check every 1 second then reset the timer
  4. write a “main loop” program to control the motors and make adjustments to the pwm until all rotation variables are equal in value
    or something like that.
    with this code i tried to do what i listed above, and like i said i have some success, but after this how would i make a loop for the motors do i need to make a loop for the motors? do i just write what i want the motors to do? is there a structure to follow?

//calling bonescript library
var b = require(‘bonescript’);

//variables used for interrupts and counting
var motor1 = ‘P9_15’;
var motor2 = ‘P9_17’;
var motor3 = ‘P8_16’;
var motor4 = ‘P8_18’;

var counter1 = 0;
var counter2 = 0;
var counter3 = 0;
var counter4 = 0;

var diskslots = 20;

//Setting all motor pins as inputs
b.pinMode(motor1, b.INPUT);
b.pinMode(motor2, b.INPUT);
b.pinMode(motor3, b.INPUT);
b.pinMode(motor4, b.INPUT);

//Interrupt Service Routines
function ISR_Count1()
{
counter1++;
}
function ISR_Count2()
{
counter2++;
}
function ISR_Count3()
{
counter3++;
}
function ISR_Count4()
{
counter4++;
}

//make motor pins external interrupts
b.attachInterrupt(motor1, true, b.CHANGE, ISR_Count1);
b.attachInterrupt(motor2, true, b.CHANGE, ISR_Count2);
b.attachInterrupt(motor3, true, b.CHANGE, ISR_Count3);
b.attachInterrupt(motor4, true, b.CHANGE, ISR_Count4);

//Function to count the rotaions and output data to console log

function motorOut()
{
b.detachInterrupt(motor1);
var rotation1 = counter1/20;
console.log("Motor 1: ", rotation1);
counter1 = 0;
b.detachInterrupt(motor2);

var rotation2 = counter2/20;
console.log("Motor 2: ", rotation2);
counter2 = 0;

b.detachInterrupt(motor3);

var rotation3 = counter3/20;
console.log("Motor 3: ", rotation3);
counter3 = 0;

b.detachInterrupt(motor4);

var rotation4 = counter4/20;
console.log("Motor 4: ", rotation4);
counter4 = 0;

}

setInterval(motorOut,1000);

sorry for the unorganizedness of this post. its midnight where im at and sleep may 5 hrs a night for the last eight months.

You don’t need interrupts for counting pulses. Instead use the hardware counters inbuild in the AM335x CPU. Those are

  • 3xPWMSS.eQEP
  • 3xPWMSS.eCAP
  • 4xTIMERSS
    Find details and example code (C, Python, BASIC) in the libpruio documentation.

Regards

this is what i was looking for. how do i access the timers that the BBB has? that link will lead me to all that?