Guest Posted January 5, 2025 Posted January 5, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
BrickTronic Posted January 5, 2025 Posted January 5, 2025 2 hours ago, Bliss said: ... But as far as I remember, the serial/Infrared link between the PC and the RCX was only to send a program to the CPU inside the RCX (and to update/upload the firmware). ... Hello, When you check the Firmware-Command-Overview that is part of the SDK (can be downloaded on Philos' Page) then for each command there is stated if this Command is "Direct", or "Program" or both. So most commands can be used inside a to RCX downloaded Program or to be executed imediate after IR-Reception. Therefore a "Reply" is usually specified, that in a Program-Execution is not needed. Jo Quote
Guest Posted January 5, 2025 Posted January 5, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Toastie Posted January 5, 2025 Posted January 5, 2025 2 hours ago, Gunners TekZone said: I believe the RCX can follow both internal and remote commands at same time. Sure it does! My entire train layout relies on that. For example, two trains have RCX' as controller on the engine or one of the carriages. I have a video on YT (https://www.youtube.com/watch?v=wTP6WtcnQbg). I can change PID parameter settings on the fly, when the trains are running, via messaging. There are a couple of tasks running on the RCX, one is for IR message handling (OK, I changed that to RF, but the protocol is exactly the same), one is for the PID speed control (there is a rotation sensor on the engine or carriage) and one is the main control program. This is done in NQC = LEGO byte codes. Without PID, they run on original LEGO firmware, with PID to work properly, the RobotC firmware is required. These are all currently available on the net. Best, Thorsten Quote
Bliss Posted January 5, 2025 Posted January 5, 2025 (edited) 8 hours ago, Gunners TekZone said: Hello. I am attempting to convert a MQTT script I have for use on my BuildHat (a LEGO Powered Up adapter for the Raspberry Pi) to the Interface-B (using its inputs/outputs instead of the BuildHat ones)... Because why not :D I have all the required modules already installed on my PC (and Python 3.10.4)... And tested good using Thonny. However, I instantly ran into import issues... Lego1: Opening Serial Port COM1... Lego1: Serial Port COM1 Opened. Lego1: Sending initialization messsage on COM1... Lego1: Reading initialization answer from COM1... Lego1: Running on COM1 No module named 'random' I also require paho.mqtt ... but as random should already be part of the base Python, and still not loading, I suspect there is more going on. Seems that the Lego script B program does not embed this module. (I'msearching if it is even possible to embed some python modules in the exe...) But from what I found out, In the publish folder of Lego Script B, where the exe is also located, there is a lib sub folder with a bunch of .py modules... So, you could do this: import sys sys.path.append(".\lib") import random print(random.random()) Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone spelling & grammar, when I catch it. And total rewrites when needed :) Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 4 hours ago, Gunners TekZone said: What version of Python is your script running? Or is there another explanation of this future feature is not defined:annotations message? Actually, I'm using IronPython and I'm not sure what version of official Python it is using. Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) I made a big change in the LegoScriptB program and the previous Examples will not work anymore. Please, use the examples provided in the README file. New version LegoScriptB Before: Lego1.SetOn[3] = True if Lego1.InOn == True: Now: Lego1.Out[3].On = True if Lego1.Inp[1].On: This allow us to do the following: MotorA = Lego1.Out[out.A] Touch1 = Lego1.Inp[1] Example: import time Lego1.ComPort="COM14" Lego1.StartLego() time.sleep(1) MotorA = Lego1.Out[out.A] Touch1 = Lego1.Inp[1] MotorA.Pow = 7 MotorA.Dir = False while not cancellationToken.IsCancellationRequested: MotorA.On = Touch1.On time.sleep(0.020) I also added a method in the outputs calle OnFor(Delay) Delay = 0-255 tenth of second. Lego1.Out[1].OnFor(20) will activate Output A of Lego1 for 2 seconds then deactivate it. It will not block the program flow, if you want to block, use after time.sleep(2) to wait 2 sec. Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) 12 minutes ago, Gunners TekZone said: Um... unless I am getting too tired... this latest version no longer allows... It still work for me. Is random.py in "C:\\Users\Gunner\AppData\Local\Programs\Python\Python313\Lib\site-packages"? Should C:\\ be C:\ ? For me random.py is in ...AppData\Local\Programs\Python\Python313\Lib\. Edited January 6, 2025 by Bliss Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) I made a small correction in LegoScriptB because there was a small glitch in the OnFor method... LegoScriptB Link For reference, here is LegoDemoB Link Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) 6 minutes ago, Gunners TekZone said: It was working with your prior version... I chose that path because all other modules I might use, like paho.mqtt, is also there for when working with other Python editors. This should work no? import sys sys.path.append("C:\\Users\Gunner\AppData\Local\Programs\Python\Python313\Lib\site-packages") sys.path.append("C:\\Users\Gunner\AppData\Local\Programs\Python\Python313\Lib") import random import paho.mqtt OR MAYBE JUST: import sys sys.path.append("C:\\Users\Gunner\AppData\Local\Programs\Python\Python313\Lib") import random import paho.mqtt Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) 5 hours ago, Gunners TekZone said: ... So I will go back to the semi-autonomous train port, from TC LOGO to python code... No fancy stuff there, that I can't understand without hours of Googling... Hah! Here is an attempt to make a script for the first part of your train program (9V train part) . In that script, You have to change the com port, CN_Auto_Speed (I set it to 7), and the LightSp = 550 (Light Setpoint). The light setpoint is very different than you program which is Light < 70. Also, this appears to be reverse than the Value I programmed for the inputs. I suspect that ControlLab software/TC Logo is using a special calculation for the Light sensor. Me, I just take the raw 10 bits value... So might be a good Idea to use the LegoDemoB program to test the your Light8 and Light7 value when a train block the sensor and the value when it clrears the sensor. Then adjust the LightSp variable accordingly. So, in "while not C() and Light8.Val < LightSp :" , when Sensor is clear (sees the light), value is around 480 (For me), and the code is looping, and when Sensor is blocked, it gives a value over 620. So the loop will end when Sensor has a value > LightSp (550) import time Lego1.ComPort="COM14" Lego1.StartLego() time.sleep(1) C=lambda:cancellationToken.IsCancellationRequested LampB = Lego1.Out[out.B] SoundC = Lego1.Out[out.C] MotorD = Lego1.Out[out.D] LampF = Lego1.Out[out.F] MotorH = Lego1.Out[out.H] Touch4 = Lego1.Inp[4] Light7 = Lego1.Inp[7] Light8 = Lego1.Inp[8] LampB.Dir=True LampB.On=True MotorD.Dir=True MotorD.OnFor(20) time.sleep(2) SoundC.Dir=True SoundC.OnFor(20) time.sleep(2) CN_Auto_Speed=7 LightSp = 550 # Light Setpoint while not C(): MotorH.Dir = True MotorH.Pow = CN_Auto_Speed MotorH.On = True while not C() and not Touch4.On: pass MotorH.On = False LampF.On = True time.sleep(1) SoundC.Dir=False SoundC.On=True MotorH.Dir = False MotorH.On = True # backup until trigger light sensor while not C() and Light8.Val < LightSp : pass MotorH.On = False LampF.On = False SoundC.On=False MotorD.Dir=False MotorD.OnFor(20) time.sleep(2) break LampB.On=False 5 hours ago, Gunners TekZone said: That is the correct path and did work on your prior version (as per your solution to the original "No module named 'random'" issue). Maybe it is because you tried first the code import sys sys.path.append(".\lib") And it worked . Then you change the path but the last one (.\lib) was still in memory (Once the parh is appended, i suspect it remains in the session even if you modify the script or clear the script textbox windows and run the script again with a new path append. But shuting down the LegoSriptB program completly must reset the memory... and when you run it again, or a new version, it will not find the random.py if you only append the ...site-packages folder because random.py does not exist in this folder. Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 (edited) 11 hours ago, Gunners TekZone said: And... Off to sleep... It has been a long many hours learning me some Python... Partial results are better than nothing I guess. Anyway, I think you're right, IronPython integration to C# is probably very minimum... But since you aready have Python 3.X on your PC, you could do the other way around... Use Lego directly into python... You could use the Dacta Python program that you found here: https://www.shamlian.net/projects/dacta/index.html I did not personnaly had time to look at it. But I'm guessing it might work very well. OR You could use my Lego DLL directly in Python but there are few step to achieve this. I did some test and I will describe the steps I followed. I'm still very newbie at Python. I compiled the Lego Interface B program as a Class Library (DLL). Only the part that manages the Interface B, No IronPython. At first it did not work. To make it work, I had to compile it using .Net Framework 4.7... 1. You have to intall Python Net from the CLI (Command Line Interface, I just opened a Dos Command Window) You type in the command line: pip install pythonnet and press enter. Make sure it has installed properly. 2. Download my LegoClassB: Link to my LegoClassB 3. In python IDLE Shell or Python CLI, enter the command: import clr If pythonnet installed correctly there should be NO Error. 4. Now enter the following: (Please note the small "r" at the beginning of the path to tell to use raw string or you may get a problem because of the use of "\") LegoClassB is the name of the DLL without the extension DLL... clr.AddReference(r"C:\...\LegoClassB\bin\Release\LegoClassB") If things goes well, Python will respond with: <System.Reflection.RuntimeAssembly object at 0x000001BEF230C9C0> 5. Enter the following: from LegoClassB import LegoInterfaceB if there are no Errors, then you are in business. 6. Enter the following one at a time to declare the Lego1 Interface and configure the com port, start and try (Do the same for Lego2, Lego3, Lego4, Lego999 lol, but first lets make it work with Lego1) Lego1 = LegoInterfaceB() Lego1.ComPort="COM1" Lego1.StartLego() print(Lego1.IsRunning) Lego1.Out[1].On=True Let me know how it goes. I think that you should have more luck with having MQTT to run with Lego box at the same time... But still, there might be some surprises... Edited January 6, 2025 by Bliss Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Bliss Posted January 6, 2025 Posted January 6, 2025 32 minutes ago, Gunners TekZone said: But you are far ahead of myself :) ... I will definitely see what I can do with your DLL (DLL, class, module?? See I don't even understand the proper terminology, Hah!) The lego Interface B code (Communication, Reading continuously and Decoding of Inputs and writing commands) is all done in what they call a C# Class which you can explore de code by opening the LegoInterfaceB.cs with notepad or Notepad++. LegoInterfaceB.cs is located in the complete zip package directly in the root folder (Ex. LegoClassB folder) When we compile a class it becomes a DLL (Library) and even in the LegoSciptB and LegoDemoB, there is a LegoInterfaceB.DLL but I tried to import it in Python and didn't work... I had to compile with a different .Net... Quote
Guest Posted January 6, 2025 Posted January 6, 2025 (edited) N/A Edited June 27 by Gunners TekZone I deleted a bunch of rambling, as this isn't a "Help with Python" topic Quote
Guest Posted January 7, 2025 Posted January 7, 2025 (edited) N/A Edited June 27 by Gunners TekZone Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.