ghp
Posts: 2001
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Programming for a ship model

Sat May 22, 2021 8:51 pm

Or is this not an issue for a stepper?
Not an issue.

Check the pulses on the output using a multimeter or attach some LED (with resistor of course).

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Wed May 26, 2021 3:42 pm

Hello Ghp,

Thanks for the help.

I have checked the pulses, I get 0V on each output. When I measure from the middle motor connector (either GND or 5V+) I do measure 5V. I tried to connect the - on the power input with the - on it's power source (the ESC for 1 of the main engines), this gave no different outcome. I tested with a brand new stepper motor as well.

Could it be that the stepper motor controller tries to control it as a bipolar motor while I'm using a unipolar motor?

Thanks in avdvance.

Greetings Josse

ghp
Posts: 2001
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Programming for a ship model

Thu May 27, 2021 5:29 am

Could it be that the stepper motor controller tries to control it as a bipolar motor while I'm using a unipolar motor?
Many things are possible, can't say for sure.

Did you check this setup on the lab bench before putting it in place ?
What are the possible problems
- power supply for the motor shield as already discussed.
- servo connected wrong
- i2c signals not/wrong connected
- duplicate address on i2c bus with the other components already mounted

My personal debugging strategy would be:
- get a copy of this board and a pi to the bench.
- connect a LED (+resistor) to one of the pwm output PWM#15, PWM#14, PWM#1, PWM0 and write a small program which makes this blink. This checks I2C and PCA9865 only.
- connect some LED to the H-Bridge output (+resistor)
- write a small program which drives the PCA9865 and thus controls the PWMA, AIN1 and AIN2 or PWMB, BIN1, BIN2 signals to have high/low/open stable for some time.
- bring this small program to the pi on the model and check if it is working with existing motorshield there
- look for the rx servo output there and sort out board power, i2c problems
- look for the servo connections.

partially tested code:

Code: Select all

import smbus
import time
import RPi.GPIO as GPIO
import threading
import math 

wait = threading.Event()
debug = True


GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme

class PWM_PCA9685 ():
    """16-channel, 12-bit PWM Fm+ I2C-bus LED controller
       Based on a script from adafruit."""
    
    __MODE1              = 0x00
    __MODE2              = 0x01
    __SUBADR1            = 0x02
    __SUBADR2            = 0x03
    __SUBADR3            = 0x04

    __LED0_ON_L          = 0x06
    __LED0_ON_H          = 0x07
    __LED0_OFF_L         = 0x08
    __LED0_OFF_H         = 0x09

    
    __ALLLED_ON_L        = 0xFA
    __ALLLED_ON_H        = 0xFB
    __ALLLED_OFF_L       = 0xFC
    __ALLLED_OFF_H       = 0xFD
    __PRE_SCALE          = 0xFE

    def __init__(self, frequency = 50, i2cbus = 1, i2c_address = 0x40, oe = 18):
 
        self.i2c_address = i2c_address
        self.i2c_bus = i2cbus
        self.frequency = frequency
        
        self.bus = smbus.SMBus(i2cbus)
        time.sleep(0.2)
        
        self.oe = oe
        GPIO.setup( self.oe, GPIO.OUT) 
        GPIO.output( self.oe, GPIO.LOW)
        
    def readU8(self, register):
        res = self.bus.read_byte_data(self.i2c_address, register)
        if debug:
            print("register", register, "res", res)
        return res
    
    def write8(self, register, value):
        print("register", register, "value", value)
        
        self.bus.write_byte_data(self.i2c_address, register, value)
            
    def setActive (self, state):
        if debug:
            print("setActive", state)
        #
        # use default implementation to start up SPI
        #
        if state:
            self._reset()
            
            self._setFrequency(
                                    self.i2c_bus, 
                                    self.i2c_address, 
                                    self.frequency
                                    )   
            
 
    def _setFrequency (self, bus, address, frequency):
        "Sets the PWM frequency"
        prescaleval = 25000000.0    # 25MHz
        prescaleval /= 4096.0       # 12-bit
        prescaleval /= float(frequency)
        prescaleval -= 1.0
        prescale = math.floor(prescaleval + 0.5)
        if debug:
            print( "Setting PWM frequency to %d Hz" % frequency )
            print ("Estimated pre-scale: %d" % prescaleval )
            
            print ("Final pre-scale: %d" % prescale)

        oldmode = self.readU8(self.__MODE1);
        #
        # go to sleep
        #
        newmode = (oldmode & 0x7F) | 0x10             # no reset, sleep
        self.write8(self.__MODE1, newmode)        # go to sleep
        
        self.write8(self.__PRE_SCALE, int(math.floor(prescale)))
        #
        # leave sleep
        #
        self.write8(self.__MODE1, oldmode)
        #
        # this time is NOT in the datasheet
        #
        time.sleep(0.005)
        self.write8(self.__MODE1, oldmode | 0x80)

    def _setPWM(self, channel, on, off):
        "Sets a single PWM channel"
        self.write8(self.__LED0_ON_L + 4*channel, on & 0xFF)
        self.write8(self.__LED0_ON_H + 4*channel, on >> 8)
        
        self.write8(self.__LED0_OFF_L + 4*channel, off & 0xFF)
        self.write8(self.__LED0_OFF_H + 4*channel, off >> 8)
        
    def _reset(self):
        self.write8(self.__MODE1, 0x00)
        
    def channel_0(self, value):
        self._channel(0, value)
        
    def channel_1(self, value):
        self._channel(1, value)
        
    def channel_2(self, value):
        self._channel(2, value)
        
    def channel_3(self, value):
        self._channel(3, value)
        
    def channel_4(self, value):
        self._channel(4, value)
        
    def channel_5(self, value):
        self._channel(5, value)
        
    def channel_6(self, value):
        self._channel(6, value)
        
    def channel_7(self, value):
        self._channel(7, value)
        
    def channel_8(self, value):
        self._channel(8, value)
        
    def channel_9(self, value):
        self._channel(9, value)
        
    def channel_10(self, value):
        self._channel(10, value)
        
    def channel_11(self, value):
        self._channel(11, value)
        
    def channel_12(self, value):
        self._channel(12, value)
        
    def channel_13(self, value):
        self._channel(13, value)
        
    def channel_14(self, value):
        self._channel(14, value)
        
    def channel_15(self, value):
        self._channel(15, value)
        
    def _channel(self, channel, value):
        try:
            v = float(value)
        except ValueError:
            return
        if v < 0.0 :
            v = 0.0
        if v > 100.0 :
            v = 100.0

        bv = (0xfff-1) / 100.0 * v 
        self._setPWM(channel, 0, int(bv))

    def servo_0(self, value):
        self._servo(0, value)
        
    def servo_1(self, value):
        self._servo(1, value)
        
    def servo_2(self, value):
        self._servo(2, value)
        
    def servo_3(self, value):
        self._servo(3, value)
        
    def servo_4(self, value):
        self._servo(4, value)
        
    def servo_5(self, value):
        self._servo(5, value)
        
    def servo_6(self, value):
        self._servo(6, value)
        
    def servo_7(self, value):
        self._servo(7, value)
        
    def servo_8(self, value):
        self._servo(8, value)
        
    def servo_9(self, value):
        self._servo(9, value)
        
    def servo_10(self, value):
        self._servo(10, value)
        
    def servo_11(self, value):
        self._servo(11, value)

    def servo_12(self, value):
        self._servo(12, value)

    def servo_13(self, value):
        self._servo(13, value)

    def servo_14(self, value):
        self._servo(14, value)

    def servo_15(self, value):
        self._servo(15, value)


    def _servo(self, channel, value):
        try:
            v = float(value)
        except TypeError:
            return
        if v < 0.0 :
            v = 0.0
        if v > 100.0 :
            v = 100.0

        bv = (0xfff-1) * ( 0.05 + 0.05 / 100.0 * v ) 
        self._setPWM(channel, 0, int(bv))

    def _output(self, channel, value):
        if value == True:
            self.write8(self.__LED0_ON_H  + 4*channel, 0b0001_0000)
            self.write8(self.__LED0_OFF_H + 4*channel, 0b0000_0000)
        if value == False:
            self.write8(self.__LED0_ON_H  + 4*channel, 0b0000_0000)
            self.write8(self.__LED0_OFF_H + 4*channel, 0b0001_0000)
            
    def output(self, channel, value):
        self._output(0, value)
        
    def output_0(self, value):
        self._output(0, value)
        
    def output_1(self, value):
        self._output(1, value)

    def output_2(self, value):
        self._output(2, value)
        
    def output_3(self, value):
        self._output(3, value)
        
    def output_4(self, value):
        self._output(4, value)
        
    def output_5(self, value):
        self._output(5, value)
        
    def output_6(self, value):
        self._output(6, value)
        
    def output_7(self, value):
        self._output(7, value)
        
    def output_8(self, value):
        self._output(8, value)
        
    def output_9(self, value):
        self._output(9, value)
        
    def output_10(self, value):
        self._output(10, value)
        
    def output_11(self, value):
        self._output(11, value)
        
    def output_12(self, value):
        self._output(12, value)
        
    def output_13(self, value):
        self._output(13, value)
        
    def output_14(self, value):
        self._output(14, value)
        
    def output_15(self, value):
        self._output(15, value)
        
class TB6612:
    H = "H"
    L = "L"
    OFF = "OFF"
    
    def __init__(self, pca9685, ic1_or_ic3):
        """IC_1 is (possibly) M1/M2
           IC_3 is (possibly) M3/M4 """
           
        self.pca9685 = pca9685
        self.ic1_or_ic3 = ic1_or_ic3
        assert ic1_or_ic3 == 'IC1' or ic1_or_ic3 == 'IC3'
        
        if ic1_or_ic3 == 'IC1':
            self.pwma = 8
            self.ain2 = 9
            self.ain1 = 10
            
            self.pwmb = 13
            self.bin2 = 12
            self.bin1 = 11
            
        if ic1_or_ic3 == 'IC3':
            self.pwma = 2
            self.ain2 = 3
            self.ain1 = 4
            
            self.pwmb = 7
            self.bin2 = 6
            self.bin1 = 5
            
    def set_a_out1_out2(self, val1, val2):
        # the pattern follows table "TB6612FNG 2012-11-01 4H-SW Control Function"
        if val1 == TB6612.L and val2 == TB6612.L:
            self.pca9685.output(self.ain1, True)
            self.pca9685.output(self.ain2, True)
            
        elif val1 == TB6612.L and val2 == TB6612.H:
            self.pca9685.output(self.ain1, False)
            self.pca9685.output(self.ain2, True)
            self.pca9685.output(self.pwma, True)

        elif val1 == TB6612.H and val2 == TB6612.L:
            self.pca9685.output(self.ain1, True)
            self.pca9685.output(self.ain2, False)
            self.pca9685.output(self.pwma, True)

        elif val1 == TB6612.OFF and val2 == TB6612.OFF:
            self.pca9685.output(self.ain1, False)
            self.pca9685.output(self.ain2, False)
            self.pca9685.output(self.pwma, True)
        else:
            print("not a valid combination 'a'", "val1", val1, "val2", val2 )

    def set_b_out1_out2(self, val1, val2):
        # the pattern follows table "TB6612FNG 2012-11-01 4H-SW Control Function"
        if val1 == TB6612.L and val2 == TB6612.L:
            self.pca9685.output(self.bin1, True)
            self.pca9685.output(self.bin2, True)
            
        elif val1 == TB6612.L and val2 == TB6612.H:
            self.pca9685.output(self.bin1, False)
            self.pca9685.output(self.bin2, True)
            self.pca9685.output(self.pwmb, True)

        elif val1 == TB6612.H and val2 == TB6612.L:
            self.pca9685.output(self.bin1, True)
            self.pca9685.output(self.bin2, False)
            self.pca9685.output(self.pwmb, True)

        elif val1 == TB6612.OFF and val2 == TB6612.OFF:
            self.pca9685.output(self.bin1, False)
            self.pca9685.output(self.bin2, False)
            self.pca9685.output(self.pwmb, True)
        else:
            print("not a valid combination 'b'", "val1", val1, "val2", val2 )
            
pca9685 = PWM_PCA9685()
pca9685.setActive(True)

pca9685.channel_0(50)
pca9685.channel_1(50)
pca9685.channel_2(50)

pca9685.channel_4(50)

for i in range(5):
    pca9685.output_0(False)
    time.sleep(1)
    pca9685.output_0(True)
    time.sleep(1)

tbb_ic1 = TB6612(pca9685, 'IC1')
tbb_ic1.set_a_out1_out2(TB6612.L,  TB6612.H)
time.sleep(1)
tbb_ic1.set_a_out1_out2(TB6612.L,  TB6612.L)
    
try:
    wait.wait()
except KeyboardInterrupt:
    pass
 
Edit: add test code, change servo test connection to PWM# close to patch area.

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Mon Jun 21, 2021 11:37 am

Hello GHP,

I'm not sure why you're talking servo's and PCA9685's here. Does 4 devices work perfectly fine. The board I'm having trouble with is the Adafruit Motor/Stepper/Servo Shield for Arduino v2 - v2.3 Kit. Since the I2C detect came up with a 0x70 adress I figured that something made a double I2C adress appear on the board so I bought myself another new board and just hooked it up and found the same problem. Reading the learn section on the Adafruit site learned that this second I2C adress is perfectly normal, it is used to talk to all the boards of this type hooked up to the same Pi or Arduino board. That throws the I2C error or malfunction idea straight out of the window.

Note that I'm using the Arduino version hooked up to a Raspberry PI. I figured that since I don't mount it on the board itself and therefor don't need to connections to match to my board and that the I2C should be the same regardless which board or other device is hooked up to it this wouldn't make any difference for getting it to work properly. The difference in price made this a tempting solution.

For the 3,3V logic the Learn on Adafruit does mention the need of using a jumper but no matter how much I search, I can't find where I need this jumper. It certainly is NOT near the power terminal block since that will make sure using the boards power to power the stepper which will probably blow up my Raspberry Pi. Can you guys help me here? I already soldered the 3,3V wire to both the 3,3V connector and the 3,3V logic pad, with no effect once again.

Thanks in advance.

Greetings Josse

ghp
Posts: 2001
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Programming for a ship model

Mon Jun 21, 2021 3:36 pm

The Adafruit Motor/Stepper/Servo Shield for Arduino v2 - v2.3 Kit has the PCA9685 on board. See the schema https://learn.adafruit.com/adafruit-mot ... /resources for more information.

The logic voltage is probably this solder jumper
Capture.PNG
Capture.PNG (35.26 KiB) Viewed 492 times
. The 5V to center seems to have a thin wire in between. The picture is not perfectly clear. You will see this better on your instance of the board. If you use the 3.3V supply, you should break this small wire... and not connect 5V to the board.
vcc.PNG
vcc.PNG (1.22 KiB) Viewed 492 times
The default connection is not shown (but I am not sure as the picture is not perfectly clear).

The code I provided is for a check of the various logic levels for the motor controller. In a 'static' way, keeping the voltages for long time. So it should be possible to measure voltages using a multimeter.

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Mon Jun 21, 2021 7:19 pm

Hello GHP,

Thanks for the help!
This indeed is the solution, the first stepper has run it's program! After that I tried stepper 2 but didn't get anything other then a little bit of noise from the stepper. Might be that I damaged the inner gears when I was drilling the hole for the screw. I'll do some more testing on that later on.

Greetings Josse

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Wed Jun 23, 2021 6:54 pm

Hello guys,

Today I found some time to do some further testing and I found that stepper 2 is indeed broken. I'm now in the process of replacing it with the brand new stepper by drilling a hole bit by bit as to not overheat the gears again. Meanwhile I also started working on a piece of code to continue my work towards a sailing vessel, so the set-up so far. Currently it doesn't give any errors but it doesn't do anything yet either. I need to think about how to translate the code into proper working order. For the steering of the ship I'm thinking about opting for values from 0 through 3600 as to have a degree value with 1 decimal without risking any errors from the decimal deviders, whilst the other 4 (stappen3 through stappen6) will have an input of 0 through 255, were 127 needs to be stop, 0 will be full reverse and 255 will be full speed ahead. Have you guys got any suggestions on how to get this working?

The "stappen" indicators in the code below are taken from my sheet with adresses and commands, so these don't need to be modified anymore.

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple test for using adafruit_motorkit with a stepper motor"""
import time
import board
from adafruit_motorkit import MotorKit 

kit1 = MotorKit(address=0x60)
kit2 = MotorKit(address=0x61)
#kit3 = MotorKit(address=0x62)

stappen1 = kit1.stepper1
stappen2 = kit1.stepper2
stappen3 = kit2.stepper1
stappen4 = kit2.stepper2
#stappen5 = kit3.stepper1
#stappen6 = kit3.stepper2

stappen = {} # empty dictionary
stappen['0B'] = stappen1
stappen['0D'] = stappen2
stappen['AB'] = stappen3
stappen['AF'] = stappen4
#stappen['AD'] = stappen5
#stappen['AA'] = stappen6

try:
  while True:
    commando = input("commando: ")
    stappen=commando[:2]
    

    
#for i in range(6000):
#    kit1.stepper1.onestep()
#   time.sleep(0.01)
finally:
	print("Goodbye!")
Thanks in advance.

Greetings Josse

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Thu Jun 24, 2021 7:04 pm

Hello guys,

I've continued with my stepper motor programming. I think I might be on a good course but I'm currently facing so much errors that I've lost track of how to fix it.
What I want to do is have a step counter which should be kept mathematically between 0 and 2048 (the number of a full rotation of the stepper motors I use), then take the input with a maximum of 3600, then recalculate in the following order: input/3600*2048. The result should be checked to the step counter so as to determine if, and if yes, how many steps and in which directions are to be made.

To make things even more interesting, I need to check wether or not the input starts with either 0B or 0D, otherwise a different calculation needs to be made but lets start with the above.

See the full code as it is now below. The errors I run into are below the code.

Can you guys help me out here?

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple test for using adafruit_motorkit with a stepper motor"""
import time
import board
from adafruit_motorkit import MotorKit 

kit1 = MotorKit(address=0x60)
kit2 = MotorKit(address=0x61)
#kit3 = MotorKit(address=0x62)

stappen1 = kit1.stepper1
stappen2 = kit1.stepper2
stappen3 = kit2.stepper1
stappen4 = kit2.stepper2
#stappen5 = kit3.stepper1
#stappen6 = kit3.stepper2

stappen = {} # empty dictionary
stappen['0B'] = stappen1
stappen['0D'] = stappen2
stappen['AB'] = stappen3
stappen['AF'] = stappen4
#stappen['AD'] = stappen5
#stappen['AA'] = stappen6

try:
  while True:
    #controller.command( 'aa120')
    commando = input("commando: ")
    stap=commando[:2]
    step=commando[2:]
    #steps=float((step) / 3600 * 2048)
    class Stappenschottel:
      def __init__(self, step, input1, stapperrotatie):
        self.step = step
        self.input1 = 3600
        self.stapperrotatie = 2048

      def calculatesteps(self):
        return step / self.input1 * self.stapperrotatie

      @classmethod
      def steps(cls, step):
        return cls(step, step)
     
      stappen = step.steps
finally:
  print; ("Goodbye!")
    
#for i in range(6000):
#    kit1.stepper1.onestep()
#   time.sleep(0.01)
#
#    print; ("Goodbye!")
Below the error I run into when running the program after giving the input 0B500:

Code: Select all

pi@Pi4W-E:~ $ /usr/bin/python3 /home/pi/Test230621.py
commando: 0B500
Traceback (most recent call last):
  File "/home/pi/Test230621.py", line 35, in <module>
    class Stappenschottel:
  File "/home/pi/Test230621.py", line 48, in Stappenschottel
    stappen = step.steps
AttributeError: 'str' object has no attribute 'steps'
Thanks in advance.

Greetings Josse

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Fri Jul 23, 2021 3:11 pm

Hello guys,

Since I couldn't make any sense of the error I ran into with regard to the translation of the number of steps I decided to pick up the pieces on the transmitter side of things. For starters I made some form of PCB made of pre- drilled board with copper covering 3 holes. I soldered some IC feet underneath in which I will later fit the IC's (both MCP3008's and MCP23017's), I made the PCB's both for the transmitter and for the boat. Then I took a length of 4-wire cable and cut that into 6 pieces of 2 different lengths, 1 for between the 2 boards that will be mounted under the topplate of the transmitter and a second set that will run to the Raspberry Pi. Why 16 wires? The first 4 for gerenal power and the I2C communication, the second set of 4 is for the SPI communication and 1 spare and the last set is for the slave select for all 4 MCP3008. This last wire is now soldered to the boards but the MCP3008's still need to be attached to it.

Meanwhile I also made a good start on the new programming that will be the interface on the new 10" touchscreen when that arrives hopefully this year. A few things have changed compared to the first attempt on this, mainly I now have room outside the tabs for the function selector and I also included a function description for each of the joysticks which chage with the selection of another function. I also programmed the commands and made a debugging list where I can actually see the command being given. Now all I need to do is make the second tab and then make it connect to the Python programming for sending the commands and to tell the Python program which function letter should be used in writing a command. This program is written using CodeTyphon.
Besturing Boot.jpeg
Besturing Boot.jpeg (135.81 KiB) Viewed 184 times
When looking in Python at my programming for the transmitter I ran into problems again as well. See code below. My father made a class for the MCP3008 but now I'm wandering wether or not that was such a good idea. The Channel Select seems to be fixed to into the code now whilst I need to change this for every MCP3008. Have you guys got any thoughts on this? Also, how do I continue from here? I mean, now I want to read all the channels and make a command out of that. Any suggestions?

Code: Select all

### System imports
import os
import time
import logging
import sys

### 3rd party imports
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
### imports local to project


class JB_MCP(object):
    def __init__(self) -> object:
        super(JB_MCP, self).__init__()
        # create the spi bus
        self.spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
        # create the cs (chip select)
        self.cs = digitalio.DigitalInOut(board.D22)
        # create the mcp object
        self.mcp = MCP.MCP3008(self.spi, self.cs)
        self.chan = (AnalogIn(self.mcp, MCP.P0), 
                    AnalogIn(self.mcp, MCP.P1), 
                    AnalogIn(self.mcp, MCP.P2), 
                    AnalogIn(self.mcp, MCP.P3),
                    AnalogIn(self.mcp, MCP.P4), 
                    AnalogIn(self.mcp, MCP.P5), 
                    AnalogIn(self.mcp, MCP.P6), 
                    AnalogIn(self.mcp, MCP.P7) )
Thanks in advance.

Greetings Josse

Tweety777
Posts: 39
Joined: Tue Jun 30, 2015 11:26 am
Location: The Netherlands

Re: Programming for a ship model

Fri Jul 23, 2021 8:02 pm

Hello guys,

I thought I could also start over with what I learned for the PCA modules and so I did, at first quite succesful while still only filling in what I already had determined in my Excel sheet. But when I'm trying to make a command I run into trouble again. I tried some things but I can't seem to get the program to understand that it needs to print for example A255, I only manage to get it to print input&value, everything else I tried gives me type errors. Can you guys help me out here?

Code: Select all

### System imports
import os
import time
import logging
import sys

### 3rd party imports
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
### imports local to project

spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs1 = digitalio.DigitalInOut(board.D7)
cs2 = digitalio.DigitalInOut(board.D12)
cs3 = digitalio.DigitalInOut(board.D13)
cs4 = digitalio.DigitalInOut(board.D15)
mcp1 = MCP.MCP3008(spi, cs1)
mcp2 = MCP.MCP3008(spi, cs2)
mcp3 = MCP.MCP3008(spi, cs3)
mcp4 = MCP.MCP3008(spi, cs4)
channel1 = AnalogIn(mcp1, MCP.P0)
channel2 = AnalogIn(mcp1, MCP.P1)
channel3 = AnalogIn(mcp1, MCP.P2)
channel4 = AnalogIn(mcp1, MCP.P3)
channel5 = AnalogIn(mcp1, MCP.P4)
channel6 = AnalogIn(mcp1, MCP.P5)
channel7 = AnalogIn(mcp1, MCP.P6)
channel8 = AnalogIn(mcp1, MCP.P7)
channel9 = AnalogIn(mcp2, MCP.P0)
channel10 = AnalogIn(mcp2, MCP.P1)
channel11 = AnalogIn(mcp2, MCP.P2)
channel12 = AnalogIn(mcp2, MCP.P3)
channel13 = AnalogIn(mcp2, MCP.P4)
channel14 = AnalogIn(mcp2, MCP.P5)
channel15 = AnalogIn(mcp2, MCP.P6)
channel16 = AnalogIn(mcp2, MCP.P7)
channel17 = AnalogIn(mcp3, MCP.P0)
channel18 = AnalogIn(mcp3, MCP.P1)
channel19 = AnalogIn(mcp3, MCP.P2)
channel20 = AnalogIn(mcp3, MCP.P3)
channel21 = AnalogIn(mcp3, MCP.P4)
channel22 = AnalogIn(mcp3, MCP.P5)
channel23 = AnalogIn(mcp3, MCP.P6)
channel24 = AnalogIn(mcp3, MCP.P7)
channel25 = AnalogIn(mcp4, MCP.P0)
channel26 = AnalogIn(mcp4, MCP.P1)
channel27 = AnalogIn(mcp4, MCP.P2)
channel28 = AnalogIn(mcp4, MCP.P3)
channel29 = AnalogIn(mcp4, MCP.P4)
channel30 = AnalogIn(mcp4, MCP.P5)
channel31 = AnalogIn(mcp4, MCP.P6)
channel32 = AnalogIn(mcp4, MCP.P7)

#Assinging commands to each input:
input = {}  # empty dictionary
input['0A'] = channel1 #MCP1 P0
input['0B'] = channel2 #MCP1 P1
input['0F'] = channel3 #MCP1 P2
input['M'] = channel4 #MCP1 P3
input['N'] = channel5 #MCP1 P4
input['O'] = channel6 #MCP1 P5
input['P'] = channel7 #MCP1 P6
input['0E'] = channel8 #MCP1 P7
input['0C'] = channel9 #MCP2 P0
input['0D'] = channel10 #MCP2 P1
input['0H'] = channel11 #MCP2 P2
input['U'] = channel12 #MCP2 P3
input['V'] = channel13 #MCP2 P4
input['W'] = channel14 #MCP2 P5
input['X'] = channel15 #MCP2 P6
input['0G'] = channel16 #MCP2 P7
input['A'] = channel17 #MCP3 P0
input['B'] = channel18 #MCP3 P1
input['C'] = channel19 #MCP3 P2
input['D'] = channel20 #MCP3 P3
input['E'] = channel21 #MCP3 P4
input['F'] = channel22 #MCP3 P5
input['Q'] = channel23 #MCP3 P6
input['R'] = channel24 #MCP3 P7
input['G'] = channel25 #MCP4 P0
input['H'] = channel26 #MCP4 P1
input['I'] = channel27 #MCP4 P2
input['J'] = channel28 #MCP4 P3
input['K'] = channel29 #MCP4 P4
input['L'] = channel30 #MCP4 P5
input['S'] = channel31 #MCP4 P6
input['T'] = channel32 #MCP4 P7

#Let's make a command here:
try:
  while True:
      #joystick = WellE2.py
      output = 'input&value'
      print(output)
    
finally:
	print("Goodbye!")
Thanks in advance

Greetings Josse

Return to “Automation, sensing and robotics”