I want to attach more IR sensors below the Formula Allcode Robot. However I am not sure to do so as I cannot get the expansion ports J16,J15 and J12 to work using the library provided using Python. How would I go on to fix this issues. Currently I cannot read or write to any of the ports.
import time
import FA
fa = FA.Create()
# make sure to use the correct COM port for your system
comport = 10
fa.ComOpen(comport)
# initialize the expansion port pin as an output
fa.ExpDDR(8, "OUTPUT") # I have tried just fa.ExpDDR(8) too
# turn on the LED on the expansion port
fa.ExpWrite(8, 1)
# wait for a second
time.sleep(1)
# turn off the LED on the expansion port
fa.ExpWrite(8, 0)
# close the communication link to the robot
fa.ComClose()
Formula AllCode Expansion ports
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Formula AllCode Expansion ports
Hello,
I'm assuming your using the expansion numbering from here.
https://www.matrixtsl.com/resources/fil ... ansion.jpg
To set the pin to an output you probably need to call this.
ExpDDR(bitnumber,direction)
bitnumber would be 8 for IO pin 8 on the FA_Expansion image and direction would be 0 for an output and 1 for an input.
The writes you are doing look fine so hopefully it's just the second parameter for the ExpDDR function that was causing problems.
I'm assuming your using the expansion numbering from here.
https://www.matrixtsl.com/resources/fil ... ansion.jpg
To set the pin to an output you probably need to call this.
ExpDDR(bitnumber,direction)
bitnumber would be 8 for IO pin 8 on the FA_Expansion image and direction would be 0 for an output and 1 for an input.
The writes you are doing look fine so hopefully it's just the second parameter for the ExpDDR function that was causing problems.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Formula AllCode Expansion ports
Hi Benj,
Thank for your reply. I have tested with directions 1 and 0.
I am not getting any voltage readings. The servomotor pins are working fine however none of the expansion ports give a reading.
this is the latest code i have used:
import time
import FA
class TestExpander:
def __init__(self):
self.robot = FA.Create()
#NOTE: make sure you use your actual COM port value here!
comport =4
self.robot.ComOpen(comport)
def test_output(self, pin):
# Set the pin as output
self.robot.ExpDDR(pin, 1)
# Write a high signal to the pin
self.robot.ExpWrite(pin, 1)
print(f"Pin {pin} is set as output and high signal is sent. Please check the voltage on the pin using a multimeter.")
time.sleep(5)
def test_input(self, pin):
# Set the pin as input
self.robot.ExpDDR(pin, 0)
# print(f"Pin {pin} is set as input. Please apply a voltage to the pin and press ENTER to continue.")
# input()
# Read the value on the pin
value = self.robot.ExpRead(pin)
print(f"Pin {pin} value: {value}")
# Read the analog value on the pin
analog_value = self.robot.ExpAn(pin)
print(f"Pin {pin} analog value: {analog_value}")
if __name__ == "__main__":
tester = TestExpander()
test_pin = 8
# Change this to the desired pin number
# Test output
tester.test_output(test_pin)
time.sleep(2)
# Test input
tester.test_input(test_pin)
CODE I ADJUSTED IN THE FA LIBRARY:
def ExpDDR(self, bitnumber, direction):
print("ExpDDR() called with bitnumber:", bitnumber, "and direction:", direction)
s = 'ExpDDR {0} {1}\n'.format(bitnumber, direction).strip()
self.__ser.write(s.encode())
# Read the response from the robot
response = self.__ser.readline().decode().strip()
print("Robot response:", response)
return
def ExpRead(self, bitnumber,timeout=100):
s = 'ExpRead {0}\n'.format(bitnumber)
self.__ser.write(s.encode())
r = self._readval("Bit", timeout)
return(r)
def ExpWrite(self, bitnumber, value):
s = 'ExpWrite {0} {1}\n'.format(bitnumber, value)
self.__ser.write(s.encode())
return
def ExpAn(self,annumber,timeout=100):
s = 'ExpAn {0}\n'.format(annumber)
self.__ser.write(s.encode())
r = self._readval("An", timeout)
return (r);
P.S when I apply voltage to the pin I either get no response or I get value: 1 no matter if im apply voltage or not. However with the E block pins when i apply voltage to light up the leds from D2 TO D8 but no reading can be capture from the code. I have tested this on 3 formula allcode robots.
Thanks,
Vic
Thank for your reply. I have tested with directions 1 and 0.
I am not getting any voltage readings. The servomotor pins are working fine however none of the expansion ports give a reading.
this is the latest code i have used:
import time
import FA
class TestExpander:
def __init__(self):
self.robot = FA.Create()
#NOTE: make sure you use your actual COM port value here!
comport =4
self.robot.ComOpen(comport)
def test_output(self, pin):
# Set the pin as output
self.robot.ExpDDR(pin, 1)
# Write a high signal to the pin
self.robot.ExpWrite(pin, 1)
print(f"Pin {pin} is set as output and high signal is sent. Please check the voltage on the pin using a multimeter.")
time.sleep(5)
def test_input(self, pin):
# Set the pin as input
self.robot.ExpDDR(pin, 0)
# print(f"Pin {pin} is set as input. Please apply a voltage to the pin and press ENTER to continue.")
# input()
# Read the value on the pin
value = self.robot.ExpRead(pin)
print(f"Pin {pin} value: {value}")
# Read the analog value on the pin
analog_value = self.robot.ExpAn(pin)
print(f"Pin {pin} analog value: {analog_value}")
if __name__ == "__main__":
tester = TestExpander()
test_pin = 8
# Change this to the desired pin number
# Test output
tester.test_output(test_pin)
time.sleep(2)
# Test input
tester.test_input(test_pin)
CODE I ADJUSTED IN THE FA LIBRARY:
def ExpDDR(self, bitnumber, direction):
print("ExpDDR() called with bitnumber:", bitnumber, "and direction:", direction)
s = 'ExpDDR {0} {1}\n'.format(bitnumber, direction).strip()
self.__ser.write(s.encode())
# Read the response from the robot
response = self.__ser.readline().decode().strip()
print("Robot response:", response)
return
def ExpRead(self, bitnumber,timeout=100):
s = 'ExpRead {0}\n'.format(bitnumber)
self.__ser.write(s.encode())
r = self._readval("Bit", timeout)
return(r)
def ExpWrite(self, bitnumber, value):
s = 'ExpWrite {0} {1}\n'.format(bitnumber, value)
self.__ser.write(s.encode())
return
def ExpAn(self,annumber,timeout=100):
s = 'ExpAn {0}\n'.format(annumber)
self.__ser.write(s.encode())
r = self._readval("An", timeout)
return (r);
P.S when I apply voltage to the pin I either get no response or I get value: 1 no matter if im apply voltage or not. However with the E block pins when i apply voltage to light up the leds from D2 TO D8 but no reading can be capture from the code. I have tested this on 3 formula allcode robots.
Thanks,
Vic