Notifications
Clear all

Questions about changing to array from buttons  

Page 8 / 8

byron
(@byron)
Honorable Member
Joined: 2 years ago
Posts: 526
 
Posted by: @gam

I found more groups for that and everyone talks about how much easier it is. 

I have found that the programming effort required, for the sort of programs one does for microcontrollers,  theres really not much difference between python and arduino c++  once one has the basics of the languages mastered.  Its not usually necessary get into advanced or expert programming for activating some relays or reading some sensors.  

The main difference to my mind is that I find it easier to read and follow the python code when one comes back to what one has written some months / years back and the code one produces is less verbose.  I also find I enjoy programming in python much more.  I may, one evening when I don't find something interesting on the box, and as an exercise for myself, take half an hour to write your 'gam circular blast gate program' in python and put it up for you to see.

I'm not aware of any python compilers that convert the code to c++, but you can write routines or bits of your program in c or c++ and then use them within your python code.   Python is implemented in c  for CPython or c++ for IronPython.  Most python interpreters are implemented in CPython. (or at least the ones I use on my Mac, Windows or Rpi computers are CPython based)  Python is also implemented in python (PyPy) but I guess to write it in python they had to have a python implemented in CPython to start with 🤫).   Micropython is written in c and was written from scratch again to implement the python language for microcontrollers.   Its quite usual to call c routines from ones python code.  I recently used a library written in c++ for a display that was written by Pimoroni (the purveyors of the display screen) that they made callable from micropython.  

I do find that when my programs get larger and I'm producing GUI's and accessing databases such as mySQL then the programming effort swings much in favour of python, and even more so if one it trying to do multi-threaded or multi-processor programming and all that sort of advanced stuff where python can make it relatively easy to accomplish.

Good to hear your program is maturing nicely.  A satisfying project I think. 👍 


GAM liked
ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 812
 

@gam

So flaps are more likely to trap debris. A well designed gate would self clean when the vacuum is on. If you are only ever going to have one tool on at a time I can see the utility in your disc arrangement although I didn't think expensive motors would be required to open and close a gate only extra control wires to your controller.

It doesn't matter if your unit of measure is in the distance travelled in stepper steps or degrees. Ultimately the degrees have to be rounded up and converted to a number of stepper steps.

If you are using a Raspberry Pi it is a no brainer to use Python. Advanced programmers can use just about any language but most electronic projects that use the RPi are written in Python. It is unlikely your project will need speeding up with a Python to C++ conversion and I would a worry about that then. Either way you need to be able to program in Python simply because most RPi projects are written using that language.

 

 

This post was modified 2 months ago 2 times by robotBuilder

GAM liked
ReplyQuote
byron
(@byron)
Honorable Member
Joined: 2 years ago
Posts: 526
 

@gam

Well consideration of the python code proved interesting as I had to decide which board to consider, and thus go with a micropython board or a Rpi. Rpi has no ADC pins, the rpi Pico has 3 and the ESP32 has a bunch. The code for a Rpi using python and the code for a microcontroller using micropython are rather different for the use of the gpio pins.

For my shop I do not want to muck about with the wiring to sense the current so my dilemma is what to do to trigger the blast gates and vacuum. Some sort of small processor with a button to wirelessly signal a main processor is an option, but I would prefer not to use batteries to power these switches. Its too noisy for voice control so I was hoping to find a kinetic type of switch that does not require batteries. I have these in my house where a kinetic switch generates enough power to signal to a receiver a good distance away and through walls etc., but the receiver is mains powered. I have not yet found a setup where the receiver is a low voltage one. If I can find one that would be my ideal setup, and I would have a kinetic button next to all my workshop tools signalling receivers that can signal the main controller board.

Anyway I decided that my setup would be based on a rpi. in the hope that this would be the better bet for a kinetic switch based solution, but this may change.

So for a quick knock up of some sort of code in python that sort of mirrors the arduino code I posted some days back, I had to assume your current sensors were linked to an MCP3008 ADC chip that the rpi reads over the SPI bus. I found a library for this in the rpi gpiozero library. In the gpiozero library there was also a stepper motor class, but I also found a very good library on github -
https://github.com/gavinlyonsrepo/RpiMotorLib
that covered several motors and drivers, and specifically the DRV8825 driver, and looked good for motor control in my future endeavours for my wood workshop project.

For what its worth, and I know its not of any real interest to you as I expect you are motoring with your arduino set-up with no intention of re-doing anything in python, I post a bit of untested python code to compare. I've tucked this program snipped away and it will prove useful reminder to me on the libraries I found.

 

 

 

import time
# import ADC MCP3008 chip library and LED meaning output pins
from gpiozero import MCP3008
from gpiozero import OutputDevice as OutPins
# import the motor driver for DRV8825
# motor driver library by Gavin Lyons - github gavinlyonsrepo/RpiMotorLib
from RpiMotorLib import RpiMotorLib

# global variables 
gCA = 0 # hold current angle of stepper motor
gDelay = 4 # hold delay in seconds for vacuum 

#define GPIO pins for DRV8825
# Microstep Resolution M0-M1-M2 -> GPIO Pins
M0 = OutPins(22)
M1 = OutPins(23)
M2 = OutPins(24)
m_pins = (M0, M1, M2)

direction = OutPins(20)
step = OutPins(21)

# Declare an instance of class and pass GPIO-PINs
mystepper = RpiMotorLib.A4988Nema(direction, step, m_pins, "DRV8825")

# pin for vacuum relay - note gpiozero pins default to low
VacPin = OutPins(4) 

# list to hold angles for adc channel no's 0-3 for 4 ADC pins on MCP3008
tool_angle = [0,45,255,270]         
    
# function to read ADC channels 1-4 until a high value is found.
def which_tool_channel():
    while(True):
        for channel in range(0-3):
            if MCP3008(channel).value >= 0.5:
                return channel
            time.sleep(0.5)

def move_servo(tool_channel):
    angle = tool_angle[tool]
    if angle == gCA:
        return True # no move required - consider done.
    
    # calculate steps to move to new angle
    # Note step type Full is 200 steps per circle.
    # Step type can be Full,Half,1/4,1/8,1/16,1/32
    if (angle - gCA > 0 and angle - gCA <= 180):
        clockwise = True
        numsteps = (angle - gCA) * 5 / 9
    elif (gCA - angle > 0 and gCA - angle > 180):
        clockwise = True
        numsteps = ((angle / 360)- gCA) * 5 / 9  
    elif (angle - gCA < 0 and angle - gCA <= 180):
        clockwise = False
        numsteps = (gCA - angle) * 5 / 9
    elif (angle - gCA > 0 and angle - gCA > 180):
        clockwise = False
        numsteps = ((gCA + 360) - angle) * 5 / 9   
    else:
        print('angle calc has not worked - abandon hope')
        return False
    
    # Note step type Full is 200 steps per circle.
    # Half is 400 steps etc.
    mystepper.motor_go(clockwise, # True=Clockwise, False=Counter-Clockwise
                     "Full" , # Step type (Full,Half,1/4,1/8,1/16,1/32)
                     numsteps, # number of steps
                     .0005, # step delay [sec]
                     False, # True = print verbose output 
                     .05) # initial delay [sec]
    
    # now store the angle just moved to as the current angle
    gCA = angle
    return True

# function to monitor the current ADC channel for the tool 
def monitor_adc(tool_channel):
    while(True):
        if MCP3008(tool_channel).value <= 0.5:
            return True
        time.sleep(0.5)
        
def stop_vac(tool_channel):
    time.sleep(gDelay)
    # check again that tool_channel is still low
    if monitor_adc(tool_channel) == True:
        VacPin.value = 0
        
#******************** program main loop starts here ***********************
try:
    while(True):
        tool = which_tool_channel()
        moved = move_servo(tool)
        # consider switchin vac on if its off
        if moved == True:
            if VacPin.value == 0:
                VacPin.value = 1
        if monitor_adc(tool):
            stop_vac(tool)
 
except KeyboardInterrupt:
    print('ctrl C pressed - its goodbye')
     
     

 

 

 


ReplyQuote
GAM
 GAM
(@gam)
Trusted Member
Joined: 6 months ago
Posts: 58
Topic starter  

@byron  

Posted by: @byron

kinetic type of switch

 I could actually recognize and follow the code you wrote,   I was surprise how short the code in the loop was.

Never knew of the kinetic switch. Would need to be attached to the tool some how?  I wonder if a motion sensor (of some type) would work. Rereading you post, why it was important that receiver needs to be low voltage?

As a side thought, I think I forgot you were from England till you used the term muck around 🧐 


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 812
 

@gam

 ... I was surprised how short the code in the loop was.

The more code in the main loop (a function) that is farmed out to other functions the shorter the code. And the more code in the other functions that are in turn farmed out to simpler functions the shorter they are as well. Each standing alone as easy to understand functions that perform some simple but meaningful task.

I like code to read as close to a human language as possible.
if VacPin.value == 0 then VacPin.value = 1
becomes,
if VaccuumPumpIsOff() then turnVacuumPumpOn()

I might have written my main Arduino version like this:

 

void loop(){

    if (aToolIsOn()) { // then
        if (toolAngle() != discAngle()) { // then
            turnOffVac();
            rotateDiscAngleToToolAngle();
        }
        turnOnVac();
    }
    else
    {
        turnOffVac();
    }
}

Another difference in my code is I use a generic function that given ANY two angles in a circle it will compute the steps to move from one angle to the next and the shortest direction to take.

This post was modified 2 months ago 2 times by robotBuilder

ReplyQuote
byron
(@byron)
Honorable Member
Joined: 2 years ago
Posts: 526
 
Posted by: @gam

Rereading you post, why it was important that receiver needs to be low voltage?

Only to keep as much as I can away from mains voltages.  I would site the buttons either near the on/off switch (with a blob or glue or tape) or at a convenient place near a work area such as my festool multifunction workbench where I swap various tools.  (this workbench is on a dust collector shared with a chop saw and trimming table saw, wheres other machinery is connected to a larger chip collector vac) .  

I'm thinking the buttons would just be to signal a control processor that ensures the correct blast gate is open, and others shut, before vac switch-on, and it would be easier to convert the receiving relay voltage to be at a level that could be used on GPIO pins of the processor.   I would also choose low voltage motors to operate the blast gates. 

And as I write I get another thought.  Perhaps I should have a blast gate that does not connect to anything but can be open as the default, and then the vac could be immediately switched on while this non connected gate is shutting and the correct gate for the tool is opening.  Something I will ponder on.

On the subject of kinetic or battery switches I may be swayed to battery ones that can last for a year or two having looked at these two youtube videos

And, as I see you are on the programming learning path, I do much agree with the suggestion of @robotbuilder that one of the main thoughts as you review your code should be how easy is it to understand when you come back to it in the months to come.   What can seem as an unnecessary over winded use of variable names or splitting code into various functions will not seem excessive when coming back to your code again to include yet another blast gate and hole in you gate selector disk. (DAMHIKT 😎)


GAM liked
ReplyQuote
Page 8 / 8