TXT, firmware>4.5.1 and Python

Alles rund um TX(T) und RoboPro, mit ft-Hard- und Software
Computing using original ft hard- and software
Forumsregeln
Bitte beachte die Forumsregeln!
Antworten
Benutzeravatar
MasterOfGizmo
Beiträge: 2720
Registriert: 30 Nov 2014, 07:44

Re: TXT, firmware>4.5.1 and Python

Beitrag von MasterOfGizmo » 05 Okt 2019, 14:14

FtRoboPy is a community project and this kind of information should probably go into the comminity forum.
Arduino für fischertechnik: ftDuino http://ftduino.de, ftDuino32 http://ftduino.de/32

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 14 Okt 2019, 10:55

Hi!
I tried python on firmware 4.6.6, but it appears to be buggy. I started with minimal test program:

Code: Alles auswählen

#!/usr/bin/env python3
import ftrobopy
import time
txt = ftrobopy.ftrobopy('127.0.0.1')
m = txt.motor(1)
m.setSpeed(512)
time.sleep(1)
m.setSpeed(0)
It works correct when I start it from ssh shell. However, when I try to load it and run it from GUI, it works only when I start it first time, after that when I press Start program, it just unloads program. To run again, I have to reboot txt...

I haven't found any documentation how to install python program on new firmware. It took me some time to figure out that I need directory /opt/knobloch/Python. And weird thing is that when I make that directory, python programs don't have to be there to be found by firmware... :)

Regards,
Martin

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 14 Okt 2019, 14:18

Hi!
I'm not sure about the build version, but it is one that should work with RoboPro 4.5.1, I downloaded it few weeks ago. Until now I didn't know that there was new build of same version. Why wasn't it named 4.6.7??
I can try new build later...
Thanks,
Martin
PS
Last weekend we gave some kids in local robotics club (10-12 years) to play with python and ftrobopy, and I think they liked it more then RoboPro. :)

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 15 Okt 2019, 13:14

@vleeuwen
All those examples should work, but in my case they don't, I found two problems. I tried new build too, problems are present on both.
1. If I don't call stopOnline before exit, I won't be able to connect in direct mode again. This does not affect online mode, and it does not affect using direct mode from command line (ssh). It affects only running python programs from GUI using direct mode. To enable direct mode again, I have to call stopOnline in online mode or from command line, after that I can connect in direct mode again.
2. When python code initialize ftrobopy class, GUI removes program from main screen and shows usual "NO PROGRAM". Program is still running in the background, I can see and kill it from shell but I can't stop it from GUI. To start program again, I have to find and load it again, there is not more "START PROGRAM" button. If I try some program that doesn't use ftrobopy, this is not happening, program finishes correct.

Regards,
Martin

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 20 Okt 2019, 15:29

Hi!

I was busy last few days, I didn't have time to play...

About __del__, it is never called, probably because there are some circular references in ftrobopy. But there are other ways to make sure that stopOnline is called at the end of script. First way is to use atexit module. It will register function that should be called at program exit. I haven't tried following examle, but something like that should work.

Code: Alles auswählen

import atexit
import ftrobopy
txt = ftrobopy.ftrobopy('auto')
atexit.register(txt.stopOnline)
# here comes rest of code
Second way is with context managers.

Code: Alles auswählen

import contextlib
import ftrobopy
@contextlib.contextmanager
def connect():
	txt = ftrobopy.ftrobopy('auto')
	yield txt
	txt.stopOnline()
with connect as txt:
	# do something
	pass
My preferred way is with context managers, and I don't know why ftrobopy class is not context manager.

But I still haven't found why are my scripts unloaded from TXT GUI when I start them. I did some tests, I can open and close port 65000, and program will stay loaded, but when I send a command over socket (I tried query status and start online) program is removed from GUI. It will continue running in the background, but GUI shows NO PROGRAM...

About python vs robopro, I am not a teacher, just a parent active in local school. :-)
I agree, RoboPro is excellent for beginners. We gave python only to kids that worked with RoboPro for some time, and already understand how to solve the problems. I think that python is a good step between RoboPro and C/C++, but this is just my opinion...

Regards,
Martin

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 20 Okt 2019, 19:21

Hi!

A little update. When I was testing communications, my txt was already in strange state. When I restarted it, I discovered that program running on txt should not send StartOnline command. When python program sends StartOnline command, it is unloaded from GUI. As ftrobopy sends StartOnline at start, it will be unloaded. I still don't understand why you can't reproduce this problem.

Regards,
Martin

martinzg
Beiträge: 9
Registriert: 18 Sep 2019, 09:54

Re: TXT, firmware>4.5.1 and Python

Beitrag von martinzg » 21 Okt 2019, 09:12

Hi!

Yes, __del__ will be called by carbage collector. Even if you have circular references, you can call mark and sweep collector to collect unreachable objects. However, ftrobopy has background thread ftTXTexchange, and as long as it is running, ftrobopy object can't be collected. Even when you say 'txt = None', object is still accessible to background thread. You must explicitly call txt.stopOnline, and it will stop background thread.

But my bigger issue is that when start online message (msg id 0x163FF61D) is send by python program running trough TXT GUI, program will be unloaded from GUI. Yesterday I wrote small replacement for ftrobopy that is not sending stop and start online messages if program is running on txt, I'm testing it and so far it works correct.

About python as intermediate step for kids, all you need to write small programs is simple editor. Working with C or C# without IDE like Eclipse or Visual Studio is PITA, and working with such IDE is even bigger PITA. Goal is not to teach kids how to use those behemoths, but how to solve the problems in easy and accessible way. But these are just my thoughts, I might be wrong... :-)

btw You mentioned that I can check with ssh what is going on on TXT. Does that mean that there is some log from GUI? Maybe log of stdout from running program?

Best regards,
Martin

Antworten