Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Using "for" loop to walk on Targets

#1
Hello, I'm new to RodoDK, today I decided to tackle the RoboDK Python API, learned a little about the first hexagon program and wanted to write my own simple program.
Meaning: to enter the FOR loop on Targets.
Problems:
1) I can't bind i-element to RDK. Element()
2) How to find the last Goal to write it into the "FOR" loop
So far I have 3 goals.
The code is shown below



Здравствуйте, я новичок в RodoDK, сегодня решил заняться API Python RoboDK, немного изучил первую программу, связанную с шестиугольником, и захотел написать свою простую программу.
Значение: для перехода в цикл FOR по Targets.
Проблемы:
1) Я не могу связать i-элемент с RDK. Элемент()
2) Как найти последнюю Цель, чтобы записать ее в цикл "FOR"
Пока что у меня есть 3 цели.
Код представлен ниже

Благодарить!
Code:
i = 1
for i in range(5):
   
    target = RDK.Item('Target %i')
    print("\n Target",i,":", target)
    robot.MoveJ(target)
print("Done")
#2
Can you share your RoboDK project? (RDK file)

You should have 5 targets in your project, from index 0 to 4, starting with Target 0, Target 1, ... Target 4.
#3
(10-16-2024, 08:05 AM)Albert Wrote: Can you share your RoboDK project? (RDK file)

You should have 5 targets in your project, from index 0 to 4, starting with Target 0, Target 1, ... Target 4.

Of course, here. I added 2 more targets so that they add up to 5
I also attached another file with the code (I don't know if you can use the RDP file without the Python file)


Attached Files
.rdk   New station (1).rdk (Size: 1.09 MB / Downloads: 181)
.py   Prog2.py (Size: 1.28 KB / Downloads: 169)
#4
You should properly convert the index to a string:
Code:
target = RDK.Item('Target %i' % i)
# or
target = RDK.Item('Target ' + str(i))
#5
(10-16-2024, 09:49 AM)Альберт Wrote: Вы должны правильно преобразовать индекс в строку:
Code:
target = RDK.Item('Target %i' % i)
# or
target = RDK.Item('Target ' + str(i))

Ой! Спасибо! Все работает! Осталось только разобраться с моментом, касающимся цикла «ЗА». Как мне связать "range()" с последней целью? Можно ли как-то рассчитать последний элемент Целей?
#6
If all you want to do is go through all targets, there is no need to fetch them by name. You can use ItemList() to get all items of type target. 

Your code would look something like this:
Code:
targets = RDK.ItemList(robolink.ITEM_TYPE_TARGET)

for t in targets:
   #yourcode
   #robot.MoveJ(t)

Doing this, you would no longer need range()

If you do want to use the name and range, you can continue what you were doing and set the range value based on the length of the list provided by  ItemList().
Code:
range(RDK.ItemList(robolink.ITEM_TYPE_TARGET))
  




Users browsing this thread:
1 Guest(s)