Advertisement
silver2row

Trying_odd_Python3_Scripts_For_Motors?

Jun 11th, 2024
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from time import sleep
  4. import os
  5. from pathlib import Path
  6.  
  7. Direction_One = Path('/sys/class/gpio/gpio44/direction')
  8. Direction_Two = Path('/sys/class/gpio/gpio45/direction')
  9. #Disable_Three = Path('/dev/gpio/P9_11/direction')
  10.  
  11. class Gpio:
  12.     def __init__( self, gpio, rw=None ):
  13. #        if type(gpio) is int:
  14.             # gpio number
  15. #            self.path = '/sys/class/gpio/gpio' + str(gpio)
  16. #        else:
  17.             # gpio name or path
  18.         self.path = os.path.join( '/sys/class/gpio/' + str(gpio))
  19.  
  20. #        if rw is not None and type(rw) is not bool:
  21. #            raise TypeError()
  22.  
  23. #        if rw:
  24. #            self._f = open(self.path + 'direction', 'r+b', buffering=0)
  25. #        else:
  26. #            self._f = open(self.path + 'direction', 'rb', buffering=0)
  27.  
  28.         if rw:
  29.             self._f = open( self.path + 'value', 'r+b', buffering=0 )
  30.         else:
  31.             self._f = open( self.path + 'value', 'rb', buffering=0 )
  32.         self.rw = rw
  33.  
  34.     @property
  35.     def value( self ):
  36.         return (b'1',b'0').index( os.pread( self._f.fileno(), 1, 0 ) )
  37.  
  38.     @value.setter
  39.     def value( self, value ):
  40.         if self.rw is None:
  41.             self._f = open( self.path + 'value', 'r+b', buffering=0 )
  42.             self.rw = True
  43.  
  44.         self._f.write( (b'1',b'0')[ value ] )
  45.  
  46. '''
  47.    @property
  48.    def direction( self ):
  49.        return (b'1', b'0').index( os.pread( self._f.fileno(), 1, 0 ) )
  50.  
  51.    @direction.setter
  52.    def direction( self, direction ):
  53.        if self.rw is None:
  54.            self._f = open( self.path + 'direction', 'r+b', buffering=0 )
  55.            self.rw = True
  56.  
  57.        self._f.write( (b'1', b'0')[ direction ] )
  58. '''
  59.  
  60.  
  61. ## example:
  62.  
  63. Direction_One.write_text('out')
  64. Direction_Two.write_text('out')
  65. #Disable_Three.write_text('in')
  66.  
  67. try:
  68.     arc = float(input("Please enter in a float! "))
  69.  
  70.     steps_to_move    = 500
  71.     step_count       = 0
  72.     direction        = 0
  73.     step_delay       = 0.1
  74.     while True:
  75.         DIR  = Gpio('gpio44/')
  76.         STEP = Gpio('gpio45/')
  77.  
  78.         print( DIR.value )
  79.         print( STEP.value )
  80.         if arc >= 1:
  81.             DIR.value  = 0
  82.             STEP.value = 1
  83.             sleep(4)
  84.         if DIR.value == direction:
  85.             STEP.value = 1
  86.             sleep(step_delay)
  87.  
  88.         step_count = step_count + 1
  89.         if step_count == steps_to_move:
  90.             step_count = 0
  91.             if direction >= True:
  92.                 direction = 0
  93.             else:
  94.                 direction = 1
  95. except KeyboardInterrupt:
  96.     STEP.value = 0
  97.     pass
  98.     print("Over and done! ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement