From a486b967c23735bc6f8fd575a8911f38f1f5b2e8 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Thu, 15 Dec 2022 21:32:14 -0500 Subject: [PATCH] Last commit didn't sync everything, trying again --- 19 - AddressableLED/Challenge/README.md | 1 + 19 - AddressableLED/Challenge/robot.py | 81 +++++++++++++++++ 19 - AddressableLED/README.md | 0 2 - Console In and Out/Challenge/README.md | 1 + 2 - Console In and Out/README.md | 9 ++ .../Challenge/README.md | 1 + 3 - Variables and Basic Math/README.md | 48 ++++++++++ 4 - If Else and Logical Operators/README.md | 87 +++++++++++++++++++ 4 - If Else and Logical Operators/main.py | 2 +- 9 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 19 - AddressableLED/Challenge/README.md create mode 100644 19 - AddressableLED/Challenge/robot.py create mode 100644 19 - AddressableLED/README.md diff --git a/19 - AddressableLED/Challenge/README.md b/19 - AddressableLED/Challenge/README.md new file mode 100644 index 0000000..ddaac5d --- /dev/null +++ b/19 - AddressableLED/Challenge/README.md @@ -0,0 +1 @@ +Make your own LED animation, be creative, no wrong answers on this challenge, assuming your code runs as you describe in comments. \ No newline at end of file diff --git a/19 - AddressableLED/Challenge/robot.py b/19 - AddressableLED/Challenge/robot.py new file mode 100644 index 0000000..b3e4a9c --- /dev/null +++ b/19 - AddressableLED/Challenge/robot.py @@ -0,0 +1,81 @@ +import wpilib +import math + +class MyRobot(wpilib.TimedRobot): + def robotInit(self): + self.width = 84 + self.height = 56 + + self.buffer = wpilib.AddressableLED.LEDData(self.width * self.height) + + self.leds = wpilib.AddressableLED(0) + self.leds.setLength(self.width * self.height) + self.leds.start() + + self.hsvShift = 0 + + self.plasma = [[0] * self.width] * self.height + + for i in range(self.width): + for j in range(self.height): + self.plasma[i][j] = int((128.0 + (128.0 * math.sin(i / 4.0)) + 128.0 + (128.0 * math.sin(j / 4.0))) / 2) + + self.palette = [0] * 256 + + for i in range(len(self.palette)): + self.palette[i] = self.__hsvToColor(self.__map(i, 0, 255, 0, 359), 1, 1) + + def robotPeriodic(self): + for i in range(self.width): + for j in range(self.height): + position = (self.plasma[i][j] + self.hsvShift) % 256 + + self.buffer.setLED((j * self.width) + i, self.palette[position]) + + self.hsvShift += 1 + self.leds.setData(self.buffer) + + def autonomousInit(self): + pass + + def autonomousPeriodic(self): + pass + + def teleopInit(self): + pass + + def teleopPeriodic(self): + pass + + def __map(self, x, inMin, inMax, outMin, outMax): + return int((x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin) + + def __hsvToColor(self, h, s, v): + if s == 0: + return wpilib.Color(wpilib.Color8Bit(int(v * 255.0), int(v * 255.0), int(v * 255.0))) + + htemp = h / 60.0 + i = int(math.floor(htemp)) + + f = htemp - i + p = v * (1.0 - s) + q = v * (1.0 - s * f) + t = v * (1.0 - s * (1.0 - f)) + + if i == 0: + return wpilib.Color(wpilib.Color8Bit(int(v * 255.0), int(t * 255.0), int(p * 255.0))) + elif i == 1: + return wpilib.Color(wpilib.Color8Bit(int(q * 255.0), int(v * 255.0), int(p * 255.0))) + elif i == 2: + return wpilib.Color(wpilib.Color8Bit(int(p * 255.0), int(v * 255.0), int(t * 255.0))) + elif i == 3: + return wpilib.Color(wpilib.Color8Bit(int(p * 255.0), int(q * 255.0), int(v * 255.0))) + elif i == 4: + return wpilib.Color(wpilib.Color8Bit(int(t * 255.0), int(p * 255.0), int(v * 255.0))) + elif i == 5: + return wpilib.Color(wpilib.Color8Bit(int(v * 255.0), int(p * 255.0), int(q * 255.0))) + else: + return wpilib.Color(0, 0, 0) + +if __name__ == "__main__": + wpilib.run(MyRobot) \ No newline at end of file diff --git a/19 - AddressableLED/README.md b/19 - AddressableLED/README.md new file mode 100644 index 0000000..e69de29 diff --git a/2 - Console In and Out/Challenge/README.md b/2 - Console In and Out/Challenge/README.md index ea2616b..f65cdb7 100644 --- a/2 - Console In and Out/Challenge/README.md +++ b/2 - Console In and Out/Challenge/README.md @@ -1 +1,2 @@ +# 2 - Console In and Out - Challenge Build a program that requests a user's name, and then prints a greeting. \ No newline at end of file diff --git a/2 - Console In and Out/README.md b/2 - Console In and Out/README.md index e69de29..12c06a9 100644 --- a/2 - Console In and Out/README.md +++ b/2 - Console In and Out/README.md @@ -0,0 +1,9 @@ +# 2 - Console In and Out + +## What is the console? +The answer to this question is "it's a place to send and receive text to and from your program". For any programs that you run from the command line (Command Prompt in Windows, Terminal in most Linux Distributions), the console is the window you see. This window can produce text from your program (Console/Standard Out) or can accept text from the keyboard (Console/Standard In). All programs technically, in some way shape or form, have a console, even if it's not run directly from Command Prompt/Terminal, but we often don't see or interact with it, because we spend most of our time interacting with our programs visually, rather than with text. + +## Why is this relevant to Robot Programming, am I going to be typing commands to my robot? +The console out piece is important to robot programming because you can use it as a mechanism to identify where your robot program "is" when it's running. You can use print() to send messages to the Driver's Station computer that you'll be able to see and use to identify issues. For example, you could place several print() statements throughout your code to identify when a "step" completes, when your robot code crashes, you can just look for which print() statement recently displayed in the console, and know roughly where in your code there may be an issue. + +You don't really use Console In in Robot Programming, but they go hand in hand, and its good to know how to do both if you want to continue to learn to program in Python. \ No newline at end of file diff --git a/3 - Variables and Basic Math/Challenge/README.md b/3 - Variables and Basic Math/Challenge/README.md index 6876a56..be63cca 100644 --- a/3 - Variables and Basic Math/Challenge/README.md +++ b/3 - Variables and Basic Math/Challenge/README.md @@ -1 +1,2 @@ +# 3 - Variables and Basic Math - Challenge Build a program that requests three numbers from the user, and performs a math operation on them and prints the result \ No newline at end of file diff --git a/3 - Variables and Basic Math/README.md b/3 - Variables and Basic Math/README.md index e69de29..62d1de1 100644 --- a/3 - Variables and Basic Math/README.md +++ b/3 - Variables and Basic Math/README.md @@ -0,0 +1,48 @@ +# 3 - Variables and Basic Math + +## Are you teaching me Algebra now? +No not really. In Algebra, you might be used to single letter variables, maybe some special characters if you've taken High School Physics already, and you're often trying to solve for one of these hidden variable values, or maybe you have a mix of known and unknown values. Python (and other programming languages) use variables to store information we want to reference in our program at a later time, the variables you'll see in programming are more dynamic that what you might have to deal with in Algebra. + +## What can variables store? +This is kind of a loaded question, the answer is a lot of things. For now, for this lesson, we're going to constrain ourselves to just talking about 4 types of variables, they are: +- Strings - This is text, usually represented by surrounding the text in double quotes +- ints (Integers) - These are whole numbers, both negative and positive and 0. You CAN'T have a decimal in an int value +- Booleans (True/False) - These are on/off, true/false values, you'll see booleans used when your code needs to make choices about what to do next +- Floats (Decimals) - These are real numbers, both negative and positive and 0. While there are limitations to what you can have for numbers here, decimal numbers are allowed and exceptionally small fractions are allowed, much smaller than what you'll need to deal with Robot Programming + +## How do I create a variable? +In the early examples, creating a variable is as easy as the following + +\ = \ +Example: fruit = "Apple" +Example: teamNumber = 2648 +Example: zuchinniNasty = True +Example: radius = 1.423563 + +When you want to use a variable in your program, simply type its name + +print(fruit) +print("Team Number: " + str(teamNumber)) + +## How do I know if a variable is a particular type? +For the most part, you don't. Python does it's best to automatically assign variables the appropriate type in the background, in the hopes that you won't have to think about it. A lot of other programming languages don't do this, as it can cause problems when trying to do certain things. + +If you are working on a part of your program and you want a value to be a certain way, most of the 4 types we talked about (with the exception of boolean) have a easy way of trying to convert on type to another + +str(value) - Will try to convert value to a string +int(value) - Will try to convert value to an integer +float(value) - Will try to convert value to a decimal + +Note that, if you try to convert something ridiculous like int("Banana"), you're going to get an error, because it really doesn't make sense to take some text, and turn it into a number. + +## Is math any different than what I'm used to? +For the most part no, you have addition, subtraction, multiplication, and division. You also have another operation, modulous, or remainder, this does a division operation, but rather than return the number of times the second number goes into the first, it returns the amount left over that couldn't be accounted for. + +(Assume we're working with only integers here) +Example: 5 / 3 = 1, 5 % 3 = 2 +Example 8 / 4 = 2, 8 % 4 = 0 + +Remainder only works on integers, floats should (but may not always) return an error. + +## What about power, cosine, sine, tangent, etc? +Those do exist! But they're a bit further into math than we need to get right now. If you want to know more, Google "Python math module". Basically, all of those things exist, but as an extra "component" you need to "import" into your code in order for them to become avaialble to use. There will be more information about importing modules later, as it becomes more important the more complicated the programs you want to create become. \ No newline at end of file diff --git a/4 - If Else and Logical Operators/README.md b/4 - If Else and Logical Operators/README.md index e69de29..f7b0179 100644 --- a/4 - If Else and Logical Operators/README.md +++ b/4 - If Else and Logical Operators/README.md @@ -0,0 +1,87 @@ +# 4 - If/Else and Logical Operators + +## What's this if/else nonsense? +Sometimes, our programs need to make decisions, we need to do one thing if some condition is true, and something else if it's not. We use if/else statements to ask questions in our program so we can make decisions about what should happen. The format of an if statement starts off simple. + +if (some condition): + (DO STUFF IF CONDITION IS TRUE) +else: + (DO STUFF IF CONDITION IS FALSE) + +We can check different things to make different choices, neat. But what do we do if we need to ask a bunch of related questions in our program? That's were elif comes in. + +if (some condition): + (DO STUFF IF CONDITION IS TRUE) +elif (some other condition): + (DO STUFF IF SOME OTHER CONDITION IS TRUE) +else: + (DO STUFF IF ALL CONDITIONS (QUESTIONS) ARE FALSE) + +elif allows use to specify another condition and some code to go with it to add another question to ask if the questions that came before it were false. This gives us more options to determine what we're going to do next. + +## Do I need to use if/elif/else all the time? +The smallest form of if is this + +if (some condition): + (DO STUFF IF CONDITION IS TRUE) + +You don't need else, you don't need elif, unless you want them and it makes sense for what you're trying to do in your program + +## Does the indentation matter? +YES! Python is ULTRA PICKY about indentation. In general, when you see a colon (:) in Python, that means that some portion of the code underneath it should be indented one tab more than the current level of indentation. Here's a very bland example (ignore the for loop for the moment, we'll talk about that next lesson) + +for x in range(10): + if x == 5 or x == 7: + print("Potato") + elif x == 2: + print("Grape") + else: + print("You get nothing") + +The identation determines something called scope. Scope is essentially defining blocks of code that "live in the same space". Scope can be a complicated topic, so we're going to gloss over it. [W3Schools](https://www.w3schools.com/python/python_scope.asp) does a really good job explaining scope relatively quickly. You can even try the code for yourself right on the webpage! + +## So how do I write a condition? +Depends on what you're trying to do. There's lots of different options, but the most common should be relatively familiar to you + +- == - Is something equal to something else + - This is not a typo, it has to be two equals signs to check if two values are the same or Python gets confused +- != - Is something not equal to something else +- < - Is the left value less than the right value +- <= - Is the left value less than or equal to the right value +- > - Is the left value greater than the right value +- >= - Is the left value greater than or equal to the right value + +Sometimes conditions are as simple as, is this less than that, or is this equal to that. Sometimes you need to check multiple things! This is where AND and OR come into play. These are logical operators that can "combine" the results of different questions (like is equal to) into a single true false value. + +AND will result in a True value only when both the left and the right side of the AND are True. + +Example: intValue < 100 and intValue > 50 is only True when intValue is any number between 51 and 99 + +The Truth Table for AND looks like this + + + + + + + + + + + + + + + + + + + + + + + + + + +
Left SideRight SideResult
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
\ No newline at end of file diff --git a/4 - If Else and Logical Operators/main.py b/4 - If Else and Logical Operators/main.py index b7f4e90..7c9d9dd 100644 --- a/4 - If Else and Logical Operators/main.py +++ b/4 - If Else and Logical Operators/main.py @@ -1,5 +1,5 @@ intValue1 = int(input("Enter Int Value 1: ")) -intValue2 = int(input("enter Int Value 2: ")) +intValue2 = int(input("Enter Int Value 2: ")) if intValue1 == intValue2: print("Int Value 1 equals Int Value 2")