chadk5utc
Posts: 10
Joined: Wed Dec 26, 2018 4:34 pm

modifying another robot of sorts PWM help

Wed Dec 26, 2018 4:53 pm

Hi all after much research I have found code that already does most of what I want it to do. I am now attempting to make changes to it to enable use of another type motor controller. currently it has 4 wired outputs(2 for each motor) for PWM and my new controller only requires 2(1 for each motor) this is where I need help as I can read and understand the basics of most code I am however unable to write, Yet. Im told by the original developer it would require only a couple lines changed but I have no idea which for sure. Any help would be much appreciated.

Code: Select all

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var exec = require('child_process').exec, child;
var port = process.env.PORT || 3000;
var ads1x15 = require('node-ads1x15');
var adc = new ads1x15(1); // set to 0 for ads1015

var Gpio = require('pigpio').Gpio,                               // I believe changes would be here? and below
  A1 = new Gpio(27, {mode: Gpio.OUTPUT}),
  A2 = new Gpio(17, {mode: Gpio.OUTPUT}),
  B1 = new Gpio( 4, {mode: Gpio.OUTPUT}),
  B2 = new Gpio(18, {mode: Gpio.OUTPUT});
  LED = new Gpio(22, {mode: Gpio.OUTPUT});

app.get('/', function(req, res){
  res.sendfile('Touch.html');
  console.log('HTML sent to client');
});

child = exec("sudo bash start_stream.sh", function(error, stdout, stderr){});

//Whenever someone connects this gets executed
io.on('connection', function(socket){
  console.log('A user connected');
  
  socket.on('pos', function (msx, msy) {
    //console.log('X:' + msx + ' Y: ' + msy);
    //io.emit('posBack', msx, msy);
	
    msx = Math.min(Math.max(parseInt(msx), -255), 255);
    msy = Math.min(Math.max(parseInt(msy), -255), 255);

    if(msx > 0){                                                                             // And changes here?
      A1.pwmWrite(msx);
      A2.pwmWrite(0);
    } else {
      A1.pwmWrite(0);
      A2.pwmWrite(Math.abs(msx));
    }

    if(msy > 0){
      B1.pwmWrite(msy);
      B2.pwmWrite(0);
    } else {
      B1.pwmWrite(0);
      B2.pwmWrite(Math.abs(msy));
    }


  });
  
  socket.on('light', function(toggle) {
    LED.digitalWrite(toggle);    
  });  
  
  socket.on('cam', function(toggle) {
    var numPics = 0;
    console.log('Taking a picture..');
    //Count jpg files in directory to prevent overwriting
    child = exec("find -type f -name '*.jpg' | wc -l", function(error, stdout, stderr){
      numPics = parseInt(stdout)+1;
      // Turn off streamer, take photo, restart streamer
      var command = 'sudo killall mjpg_streamer ; raspistill -o cam' + numPics + '.jpg -n && sudo bash start_stream.sh';
        //console.log("command: ", command);
        child = exec(command, function(error, stdout, stderr){
        io.emit('cam', 1);
      });
    });
    
  });
  
  socket.on('power', function(toggle) {
    child = exec("sudo poweroff");
  });
  
  //Whenever someone disconnects this piece of code is executed
  socket.on('disconnect', function () {
    console.log('A user disconnected');
  });

  setInterval(function(){ // send temperature every 5 sec
    child = exec("cat /sys/class/thermal/thermal_zone0/temp", function(error, stdout, stderr){
      if(error !== null){
         console.log('exec error: ' + error);
      } else {
         var temp = parseFloat(stdout)/1000;
         io.emit('temp', temp);
         console.log('temp', temp);
      }
    });
    if(!adc.busy){
      adc.readADCSingleEnded(0, '4096', '250', function(err, data){ //channel, gain, samples
        if(!err){          
          voltage = 2*parseFloat(data)/1000;
          console.log("ADC: ", voltage);
          io.emit('volt', voltage);
        }
      });
    }
  }, 5000);

});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

User avatar
MrYsLab
Posts: 375
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: modifying another robot of sorts PWM help

Thu Dec 27, 2018 4:52 pm

This is javascript code so this is probably not the best place to post your question.

chadk5utc
Posts: 10
Joined: Wed Dec 26, 2018 4:34 pm

Re: modifying another robot of sorts PWM help

Fri Dec 28, 2018 3:42 pm

Thanks for noting that I really didnt know, but as I cant find a way to delete or remove this post and hate to cross post/duplicate Ill ride this out and see if I get anything.

PhatFil
Posts: 1368
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: modifying another robot of sorts PWM help

Fri Dec 28, 2018 4:01 pm

i dont know node.js very well but i suspect just going by your Question that perhaps comment out one of the A1 or A2 write lines and one of the B1 or B2 write lines ?? whichever corresponds to unused gpio pins..

if i am shooting wide its a place to start... :twisted:

Return to “Python”