1

I am working through the 'Ultimate Starter Kit" for my Raspberry Pi 3+. In Chapter 16 the tutorial shows how to work with a stepper motor.

Note: The program works per the tutorial -

I just cannot understand how the following line is working:

GPIO.output(motorPins[i],((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))

motorPins is a four-element array with a hex number in each element.

How is this line evaluated?

tlfong01
  • 4,665
  • 3
  • 10
  • 24
Goose23
  • 41
  • 4
  • That statement is meaningless without context; is a general programming question not specific to the Pi. If you want to learn programming try to avoid complex statements like that (which are poor practice) and try to break down into each component. – Milliways Feb 16 '20 at 22:22
  • Hi @Goose23, Welcome and nice to meet you. Ah, let me see. So l I followed your tutorial and skimmed Chapter 16. I read the program listing and found it good, structured and comments nice. You should agree with me IF AND ONLY IF you have done the following 3 things: – tlfong01 Feb 17 '20 at 03:09
  • (1) Spend perhaps at least 30 minutes reading a tutorial of step motor basics: Step Motor tutorial https://raspberrypi.stackexchange.com/questions/97975/controlling-28byj48-bipolar-convert-with-l293d-ic, (2) Spend 30 minutes to read a python tutorial to understand the meanings of (a) the left shift operator "<<", (b) the logical operator "AND", and "OR", (3) Skim the program listing to get an overview of what is going on, and then look closely the function "moveOnePeriod(direction, ms)" containing the weird statement which will find not that hard to understand after all. – tlfong01 Feb 17 '20 at 03:09
  • (3) Step Motor Programming Learning Notes penzu link: https://penzu.com/p/0ea8554d. Happy python programming. Cheers :) – tlfong01 Feb 17 '20 at 03:09
  • PS - For the forum discussion I referred above,. you might like to spend more time, perhasp another 30 minutes on Lady Ada's newbie friendly tutorial on stepping motors (see the reference list for Lady Ada and also Prof Jones' tutorial if you want to dig deeper). The chat record of my answer also has a more detailed discussion on how to use the four GPIO pins to energize the motor coil in sequences (Half step, Full step, Clockwise, Counter Clockwise etc). Happy reading and learning. Cheers. – tlfong01 Feb 17 '20 at 03:21
  • And if you can answer the question in Appendix B of my learning notes above, then you might now think that the weird statement you have been so far swearing at half of the Galaxy is so elegant, and as concise as the Einstein's statement E = M * C^2, or Newton's F = M * A : ) – tlfong01 Feb 17 '20 at 07:06
  • I agree that the above code is concise, but not very readable, unless you have a sharp and clear mind, otherwise it is easy to go wrong with the confusing "<<", "AND", "OR" operators. So to play very safe, it is often not ot be very clever and use the KISS principle to write simple, but hard to go wrong code, especially for the first version. So I have included an Arduino code in Appendix C to compare and contrast with the original code. The Arduino code is not very well documented. So I ma thinking of modifying it later. – tlfong01 Feb 17 '20 at 08:58

1 Answers1

3

GPIO.output(motorPins[i],((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))

GPIO.output is a function which takes two parameters.

The first motorPins[i] specifies the GPIO to act upon. In this case it is entry i in the array motorPins.

The second ((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW) specifies the level to assign to the GPIO.

If the expression evaluates to True the GPIO will be set high otherwise it will be set low.

I'm not going to try to work through that expression as it depends on CCWStep.

I would say it is (in my opinion) terrible code and a good example of code you should never write yourself.

joan
  • 71,024
  • 5
  • 73
  • 106
  • 1
    Thank You. I understand the part you explained and am grateful I am not the only one thinking the code is obtuse. – Goose23 Feb 16 '20 at 23:48
  • i could be wrong about this ..... i think that it has to do with python data type casting and with logic expression short circuiting .... the CCWStep[j] == 1<<i evaluates to true or false ... the boolean operator and converts the result to the same data type as GPIO.HIGH .... the problem is that the AND operator is not evaluated when the expression is false, so the OR operator is required ...i wonder if the AND operator is needed ... i also wonder if reversing the AND and OR sections would still work – jsotola Feb 17 '20 at 18:23