Page 1 of 1

Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 7:00 am
by Leyland Claret
My lad has a project to complete for his A level in Design and Engineering. He is currently struggling to complete it and his tutors, how can say this…, aren’t being very helpful :evil: He has all the physical items but is struggling with the programming and coding side of things. Can you help :?: Hints, tips or actually speak/email with someone who has the knowledge to help. As you can imagine I am not particularly happy with the college and also working through the pandemic slightly hindered things…a lot!!

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 7:19 am
by Rick_Muller
Use chatGPT to ask specific tech questions- you’ll get answers you need. What exactly does he need to do?

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 7:26 am
by Leyland Claret
Rick_Muller wrote:
Sun Mar 19, 2023 7:19 am
Use chatGPT to ask specific tech questions- you’ll get answers you need. What exactly does he need to do?
He has used chatGPT but his college tutor told him it was all wrong! Not certain exactly what he needs to do but can find out. Is this something you could assist with if I get some details?

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 7:33 am
by Rick_Muller
Leyland Claret wrote:
Sun Mar 19, 2023 7:26 am
He has used chatGPT but his college tutor told him it was all wrong! Not certain exactly what he needs to do but can find out. Is this something you could assist with if I get some details?
By all means post some details, it’s not my specialism though but others may now more. So much of learning these days is knowing how to gather and use the right info from what is available, and I suspect the task in hand will test that process.

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 8:56 am
by Panthro
Codecademy is a good place to start learning the fundamentals of programming using various languages, here is the Python link: -https://www.codecademy.com/learn/learn-python-3

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 9:02 am
by Burnley1989
I’ll bet my applications engineer could help, he’s an absolute genius, I’ll tap him up tomorrow.
He built his own Raspberry set up to track satellites, just to let companies know if their satellite isn’t working correctly, I have to sit and listen to him talking me through it, I love his passion but my god it’s boring 😂

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 9:05 am
by Hurstwood_Claret
Rick_Muller wrote:
Sun Mar 19, 2023 7:19 am
Use chatGPT to ask specific tech questions- you’ll get answers you need. What exactly does he need to do?
I know a lot of colleges have banned Chat GPT

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 9:13 am
by Croydon Claret
I'm a lead software developer but I don't have any Python experience.

Is the issue related to not understanding Python code or is it more with not knowing what the required output is supposed to look like and no amount of coding is going to produce the correct end result?

Often with programming the issue isn't with the actual code but the fact that you haven't understood the requirements and then you just end up getting stressed and wasting your time because you don't really know what you're supposed to be doing.

And if you don't understand what the end goal is supposed to be then no amount of amazing world leading programming is going to produce the correct result.

Turn the computer off, stop thinking about Python and coding, and just write down in plain English what the exercise is supposed to achieve, what inputs are available, and what the output is supposed to be. All in English NO PROGRAMMING or even thinking about solutions.

Then once you have that you break the overall solution down into smaller steps, again in plain English. Only then can you start thinking about code.

Example:
A requirement comes in whereby a parameter is supplied and you need to to compare it against data from a specific file. You are required to return any line that ends with the supplied parameter.

So you would sit down and come to the conclusion that somehow you need to open a file, iterate over each line, perform a comparison of each line, and add any matches to list of matching lines to be returned

Now you can fire up the computer and write your basic understanding as comments only, no actual code at this stage

So that would give you something like

# Open the file
# Read the file contents and store in a variable
# Close the file
# Create an empty variable to hold the list of matching items
# Iterate over each line from the data you read into the variable
# If the current line ends with the supplied parameter then add the whole line to the return list
# Return the populated list of matching lines

And as you get into it you can probably break most steps down into substeps such as adding formatting, error checking, what to do if the file doesn't exist etc

You could take that construct and it would work for any programming language ever invented. You then just need to find the specific Python command for how to perform each step, and that's the easy bit because everything is on Google 👍

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 9:20 am
by Leyland Claret
Thank you all for your help so far. As you can imagine it’s quite a stressful time coming up to his A level exams. This project is 50% towards his final result and if he doesn’t get a mark in the practical he can virtually kiss goodbye to getting the grade he needs. Any more input is much appreciated. Thank you again.

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 10:07 am
by Billyblah
And here was me thinking that this was reference to the long lost Python film "Raspberry Pi"

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 11:01 am
by ClaretPete001
Depends what he is doing. This is a simple object oriented AI: Machine learning linear regression model using Pandas, matplotlib, sklearn etc.

He can have it but I'd struggle to help time wise.

It's starting off with the libraries, then opening a CSV and database, designing the model, fitting the model and finally allowing the model to learn from the data.

To be honest there are loads of sites for Python just nick some code and get it working.

import pyodbc
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
import seaborn as sns
import sys
import statistics

class Querydata:

def Connect_to_database():
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};Trusted_Connection=Yes;SERVER=IR- _____________;DATABASE=_________;TrustServerCertificate=Yes')
cursor = conn.cursor()
cursor.execute('SELECT * FROM D_DETAILS')
Records = cursor.fetchall()
return Records



#def Importdata():
# dfAchievement_data = pd.read_csv(r'C:\Users\________\project_data\_________.csv')
# print(dfAchievement_data)
# return dfAchievement_data

class Linear_regression:

#constructor

# Calling main function
# if __name__ == "__main__":
# main

def __init__(self):

# Data extraction

dfAchievement_data = Querydata.Connect_to_database()

#Data pre-processing
self.Check_for_nulls(dfAchievement_data)
self.Generatevisuals(dfAchievement_data)
dfAchievement_data = self.outlier_removal(dfAchievement_data)
print('dfAchievement_data', dfAchievement_data)
self.ReGeneratevisuals(dfAchievement_data)

# Identifiying the x and y variables
x, y = self.creatingxandyvariables(dfAchievement_data)
self.CheckforXandy(x, y)

# Training the data
x_train, x_test, y_train, y_test = self.splitdataset(x, y)
print('print before create', x_train, y_test)
Fitted_model = self.Createmodel(x_train, y_train)

# Creating the model
self.Prediction(Fitted_model,x_test, y_test)
coefficients, intercept = self.Assessment(Fitted_model)

# Implementing the model
Predicted_writing_score = self.calculate_charges(72, coefficients, intercept)
print("calculate_charges", Predicted_writing_score)

#Triangulaing the model
self.check_calculated_score(x,y)
sys.exit()

def Check_for_nulls(dfAchievement_data):
pd.isna(dfAchievement_data)
rpisnull = pd.isnull(dfAchievement_data)
print("Are there any nulls", rpisnull)
pd.DataFrame.isna(dfAchievement_data)


def Generatevisuals(dfAchievement_data):
dfAchievement_data.plot(x="gender", y="math score", kind="bar")
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def outlier_removal(dfAchievement_data):
Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("Reading upper limit: ", Reading_upper_limit)
print("Reading Lower Limit: ", Reading_lower_limit)
print("Writing upper limit: ", Writing_upper_limit)
print("Writing Lower Limit: ", Writing_lower_limit)
dfAchievement_data = dfAchievement_data[(dfAchievement_data['reading score'] > Reading_lower_limit) & (dfAchievement_data['reading score'] < Reading_upper_limit)]
dfAchievement_data = dfAchievement_data[(dfAchievement_data['writing score'] > Writing_lower_limit) & (dfAchievement_data['writing score'] < Writing_upper_limit)]

Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("New Reading upper limit: ", Reading_upper_limit)
print("New Reading Lower Limit: ", Reading_lower_limit)
print("New Writing upper limit: ", Writing_upper_limit)
print("New Writing Lower Limit: ", Writing_lower_limit)
return dfAchievement_data


def ReGeneratevisuals(dfAchievement_data):
dfAchievement_data.plot(x="reading score", y="writing score", kind="bar")
# Instantiating a LinearRegression Model
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def creatingxandyvariables(dfAchievement_data):

x = dfAchievement_data['reading score']
y = dfAchievement_data['writing score']
print(x, y)
return x, y


def CheckforXandy(x, y):

print('Returned variable x', x)
print('Returned variable y', y)

def splitdataset(x, y):

x_train, x_test, y_train, y_test = train_test_split(
x, y, shuffle=True, train_size=0.3)
return x_train, x_test, y_train, y_test

def Createmodel(x_train, y_train):

print('in creatmodel func',x_train, y_train)
x_train = x_train.array.reshape(-1,1)
model = LinearRegression()
Fitted_model= model.fit(x_train, y_train)
return Fitted_model

def Prediction(model, x_test, y_test):

predictions = model.predict(x_test.array.reshape(-1,1))
r2 = r2_score(y_test, predictions)
rmse = mean_squared_error(y_test, predictions, squared=False)
print('The r2 is: ', r2)
print('The rmse is: ', rmse)


def Assessment(model):

coefficients = model.coef_
intercept = model.intercept_
return coefficients, intercept

def calculate_charges(Rdscore, coefficients, intercept):

print('Reading score', Rdscore)
return (Rdscore * coefficients) + intercept

def check_calculated_score(x,y):

print('Statistics mean x',statistics.mean(x))
print('Statistic mean y', statistics.mean(y))

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 1:00 pm
by Leyland Claret
ClaretPete001 wrote:
Sun Mar 19, 2023 11:01 am
Depends what he is doing. This is a simple object oriented AI: Machine learning linear regression model using Pandas, matplotlib, sklearn etc.

He can have it but I'd struggle to help time wise.

It's starting off with the libraries, then opening a CSV and database, designing the model, fitting the model and finally allowing the model to learn from the data.

To be honest there are loads of sites for Python just nick some code and get it working.

import pyodbc
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
import seaborn as sns
import sys
import statistics

class Querydata:

def Connect_to_database():
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};Trusted_Connection=Yes;SERVER=IR- _____________;DATABASE=_________;TrustServerCertificate=Yes')
cursor = conn.cursor()
cursor.execute('SELECT * FROM D_DETAILS')
Records = cursor.fetchall()
return Records



#def Importdata():
# dfAchievement_data = pd.read_csv(r'C:\Users\________\project_data\_________.csv')
# print(dfAchievement_data)
# return dfAchievement_data

class Linear_regression:

#constructor

# Calling main function
# if __name__ == "__main__":
# main

def __init__(self):

# Data extraction

dfAchievement_data = Querydata.Connect_to_database()

#Data pre-processing
self.Check_for_nulls(dfAchievement_data)
self.Generatevisuals(dfAchievement_data)
dfAchievement_data = self.outlier_removal(dfAchievement_data)
print('dfAchievement_data', dfAchievement_data)
self.ReGeneratevisuals(dfAchievement_data)

# Identifiying the x and y variables
x, y = self.creatingxandyvariables(dfAchievement_data)
self.CheckforXandy(x, y)

# Training the data
x_train, x_test, y_train, y_test = self.splitdataset(x, y)
print('print before create', x_train, y_test)
Fitted_model = self.Createmodel(x_train, y_train)

# Creating the model
self.Prediction(Fitted_model,x_test, y_test)
coefficients, intercept = self.Assessment(Fitted_model)

# Implementing the model
Predicted_writing_score = self.calculate_charges(72, coefficients, intercept)
print("calculate_charges", Predicted_writing_score)

#Triangulaing the model
self.check_calculated_score(x,y)
sys.exit()

def Check_for_nulls(dfAchievement_data):
pd.isna(dfAchievement_data)
rpisnull = pd.isnull(dfAchievement_data)
print("Are there any nulls", rpisnull)
pd.DataFrame.isna(dfAchievement_data)


def Generatevisuals(dfAchievement_data):
dfAchievement_data.plot(x="gender", y="math score", kind="bar")
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def outlier_removal(dfAchievement_data):
Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("Reading upper limit: ", Reading_upper_limit)
print("Reading Lower Limit: ", Reading_lower_limit)
print("Writing upper limit: ", Writing_upper_limit)
print("Writing Lower Limit: ", Writing_lower_limit)
dfAchievement_data = dfAchievement_data[(dfAchievement_data['reading score'] > Reading_lower_limit) & (dfAchievement_data['reading score'] < Reading_upper_limit)]
dfAchievement_data = dfAchievement_data[(dfAchievement_data['writing score'] > Writing_lower_limit) & (dfAchievement_data['writing score'] < Writing_upper_limit)]

Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("New Reading upper limit: ", Reading_upper_limit)
print("New Reading Lower Limit: ", Reading_lower_limit)
print("New Writing upper limit: ", Writing_upper_limit)
print("New Writing Lower Limit: ", Writing_lower_limit)
return dfAchievement_data


def ReGeneratevisuals(dfAchievement_data):
dfAchievement_data.plot(x="reading score", y="writing score", kind="bar")
# Instantiating a LinearRegression Model
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def creatingxandyvariables(dfAchievement_data):

x = dfAchievement_data['reading score']
y = dfAchievement_data['writing score']
print(x, y)
return x, y


def CheckforXandy(x, y):

print('Returned variable x', x)
print('Returned variable y', y)

def splitdataset(x, y):

x_train, x_test, y_train, y_test = train_test_split(
x, y, shuffle=True, train_size=0.3)
return x_train, x_test, y_train, y_test

def Createmodel(x_train, y_train):

print('in creatmodel func',x_train, y_train)
x_train = x_train.array.reshape(-1,1)
model = LinearRegression()
Fitted_model= model.fit(x_train, y_train)
return Fitted_model

def Prediction(model, x_test, y_test):

predictions = model.predict(x_test.array.reshape(-1,1))
r2 = r2_score(y_test, predictions)
rmse = mean_squared_error(y_test, predictions, squared=False)
print('The r2 is: ', r2)
print('The rmse is: ', rmse)


def Assessment(model):

coefficients = model.coef_
intercept = model.intercept_
return coefficients, intercept

def calculate_charges(Rdscore, coefficients, intercept):

print('Reading score', Rdscore)
return (Rdscore * coefficients) + intercept

def check_calculated_score(x,y):

print('Statistics mean x',statistics.mean(x))
print('Statistic mean y', statistics.mean(y))
Thank you so much! You sound as though you are thoroughly clued up which is amazing. I have sent this through to my lad. What he needs to know is that he has to do it himself but just needs guiding. Would it at all be possible to get in touch through email if he has any queries? I appreciate you have said you are struggling time wise and understand if it’s not possible. Many thanks. Lee

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 1:27 pm
by brexit
Hi Leyland

I assume your son is at Runshaw so he will be doing the T Level - Design Engineering?

if so, building and designing a "something" may be more appropriate - check out

https://store.arduino.cc/products/ardui ... X0QAvD_BwE

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 2:14 pm
by ClaretPete001
brexit wrote:
Sun Mar 19, 2023 1:27 pm
Hi Leyland

I assume your son is at Runshaw so he will be doing the T Level - Design Engineering?

if so, building and designing a "something" may be more appropriate - check out

https://store.arduino.cc/products/ardui ... X0QAvD_BwE
Good point, I don't know what he is studying. I think M'ch Learning is part of the Computer Science T Level and it usually forms some aspect of Undergraduate study at L4.

If you find out what his project is supposed to be about and what tools he is using then might be able to help.

If he want some simple code to read a text file (CSV) and create a machine learning Python programme to make predictions then the above will help.

For example, if he wants to get Ashley Barnes goal scoring record then he could use the above to predict when he is next likely to score. Course, Ash might be so random you can't get any mathematic correlation but I guess you could mess with the data and make it work I doubt any examiner is going to check.

But it all depends what his project is on.

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 2:20 pm
by pushpinpussy
Leyland Claret wrote:
Sun Mar 19, 2023 7:00 am
My lad has a project to complete for his A level in Design and Engineering. He is currently struggling to complete it and his tutors, how can say this…, aren’t being very helpful :evil: He has all the physical items but is struggling with the programming and coding side of things. Can you help :?: Hints, tips or actually speak/email with someone who has the knowledge to help. As you can imagine I am not particularly happy with the college and also working through the pandemic slightly hindered things…a lot!!
don't take this the wrong way, but if your son needs his dad to come on a football message board asking for assistance in this matter then I'm not sure this is the career for him going forward.

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 2:24 pm
by Rowls
Good to see proper coding being learned by the youth of the country.

Good luck to your lad Leyland and to anybody else studying and learning coding / engineering.

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 2:29 pm
by Leyland Claret
pushpinpussy wrote:
Sun Mar 19, 2023 2:20 pm
don't take this the wrong way, but if your son needs his dad to come on a football message board asking for assistance in this matter then I'm not sure this is the career for him going forward.
Thank you for your reply. He didn’t ask or know that I had requested some assistance in this. Due to the pandemic and a rather lacklustre approach by the college and the revolving door of tutors it has had things haven’t gone particularly smoothly the last couple of years. This includes his current tutor who is also leaving shortly and told some of his students that he didn’t care how things went as he was also leaving. Again I appreciate your input on something that didn’t need that type of response

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 2:30 pm
by Leyland Claret
Rowls wrote:
Sun Mar 19, 2023 2:24 pm
Good to see proper coding being learned by the youth of the country.

Good luck to your lad Leyland and to anybody else studying and learning coding / engineering.
Thank you Rowls. More like the type of post I like to read…

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 3:46 pm
by Leyland Claret
ClaretPete001 wrote:
Sun Mar 19, 2023 2:14 pm
Good point, I don't know what he is studying. I think M'ch Learning is part of the Computer Science T Level and it usually forms some aspect of Undergraduate study at L4.

If you find out what his project is supposed to be about and what tools he is using then might be able to help.

If he want some simple code to read a text file (CSV) and create a machine learning Python programme to make predictions then the above will help.

For example, if he wants to get Ashley Barnes goal scoring record then he could use the above to predict when he is next likely to score. Course, Ash might be so random you can't get any mathematic correlation but I guess you could mess with the data and make it work I doubt any examiner is going to check.

But it all depends what his project is on.
Hiya pal. My lad has sent me this which obviously makes no sense to me! I have also been told it’s a Raspberry Pi Pico. Any help would be brilliant.

# read the Adafruit NeoTrellis keypad
# connect to 4 digit seven segment LED display
# numbers inputted on Adafruit display on LED display
# countdown displayed, counts backwards in the form 00:00
# countdown ends, two red LEDs turn on

Cheers

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 4:10 pm
by brexit
Leyland Claret wrote:
Sun Mar 19, 2023 3:46 pm
Hiya pal. My lad has sent me this which obviously makes no sense to me! I have also been told it’s a Raspberry Pi Pico. Any help would be brilliant.

# read the Adafruit NeoTrellis keypad
# connect to 4 digit seven segment LED display
# numbers inputted on Adafruit display on LED display
# countdown displayed, counts backwards in the form 00:00
# countdown ends, two red LEDs turn on

Cheers
As I suspected, this is machine level programming not machine learning
Everything he needs is here https://learn.adafruit.com/adafruit-neo ... ython-docs

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 4:35 pm
by Croydon Claret
It's good that he's broken it down into easy steps, now he can do them one by one with clarity

Some code for reading from the keyboard is covered here. The overall program then goes on to do different things but you should be able to rip off the keyboard input section and bin most of the remainder (there is though a handy section that explains how to light up the LEDs, which he might need for later)
https://learn.adafruit.com/adafruit-neo ... ython-code

Just get it the stage where that's working and he can print the inputted values out to his screen before worrying about anything else

Assuming he's captured the inputted values then there's code here that tells you how to send that to the display.

https://peppe8o.com/how-to-control-a-4- ... th-python/

Don't about the counting down logic, until you've got a static display working.

Big mistake that people often make is to try to write the entire program all at once. Only proceed to the next step once your happy your current little step is working

Re: Engineering project - Python with Raspberry Pi

Posted: Sun Mar 19, 2023 6:11 pm
by Croydon Claret
Some basic programming tips to help him along, in no particular order.

1) Take versioned backups. Don't just keep saving the same file over and over again. You can 100% guarantee that he'll change something then suddenly all his previous hard work has stopped executing. Best case you can spend hours figuring out what you broke, worst case you may never figure it out

Wherever he's storing his code then create subfolders and call them "version 1", "version 2" etc. When he's happy that he's made good progress then he can save the entire thing as a new version. If he screws up the code (ask me how I know 😭) then he only has to go back to the last known working version

2) Add comments before each little section of code to describe what it's doing. When you're in the zone and it's all like "A Beautiful Mind" then the code all makes sense. Come back to it in the morning and often you have no clue what drugs you were on when you wrote that code the previous day 😟

3) Leave a note at the end of the day re where you're at and what you need to do next. Often you come back in the morning then whilst the code might be commented nicely and understandable you can't remember what the hell the next steps are.

4) Don't write too much code at once without testing what you have done so far. Personally I might write half a dozen lines then give it a quick test before continuing. It's a lot easier to check you're going down the write track. If you change code all over the place and it all stops working it can be a nightmare figuring out which change broke the code.

5) Try to put distinct and/or repeatable blocks of code into functions that can be called from the main running code. You don't want a routine that's 1000 lines long, it's too hard to read and maintain. ClaretPete's earlier code is a good example of using functions. If he just wants to get the code written because he's still learning then maybe don't worry so much about this right now if it makes it too difficult, but it's definitely something he would want to look at later. Once he has a working version then he can refactor it later to make it neater 👍

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 5:30 pm
by ClaretPete001
Leyland Claret wrote:
Sun Mar 19, 2023 3:46 pm
Hiya pal. My lad has sent me this which obviously makes no sense to me! I have also been told it’s a Raspberry Pi Pico. Any help would be brilliant.

# read the Adafruit NeoTrellis keypad
# connect to 4 digit seven segment LED display
# numbers inputted on Adafruit display on LED display
# countdown displayed, counts backwards in the form 00:00
# countdown ends, two red LEDs turn on

Cheers
Oh ok looks like you've got some good advice above.

Best of luck.....

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 5:35 pm
by CoolClaret
Croydon Claret wrote:
Sun Mar 19, 2023 6:11 pm
Some basic programming tips to help him along, in no particular order.

1) Take versioned backups. Don't just keep saving the same file over and over again. You can 100% guarantee that he'll change something then suddenly all his previous hard work has stopped executing. Best case you can spend hours figuring out what you broke, worst case you may never figure it out

Wherever he's storing his code then create subfolders and call them "version 1", "version 2" etc. When he's happy that he's made good progress then he can save the entire thing as a new version. If he screws up the code (ask me how I know 😭) then he only has to go back to the last known working version

2) Add comments before each little section of code to describe what it's doing. When you're in the zone and it's all like "A Beautiful Mind" then the code all makes sense. Come back to it in the morning and often you have no clue what drugs you were on when you wrote that code the previous day 😟

3) Leave a note at the end of the day re where you're at and what you need to do next. Often you come back in the morning then whilst the code might be commented nicely and understandable you can't remember what the hell the next steps are.

4) Don't write too much code at once without testing what you have done so far. Personally I might write half a dozen lines then give it a quick test before continuing. It's a lot easier to check you're going down the write track. If you change code all over the place and it all stops working it can be a nightmare figuring out which change broke the code.

5) Try to put distinct and/or repeatable blocks of code into functions that can be called from the main running code. You don't want a routine that's 1000 lines long, it's too hard to read and maintain. ClaretPete's earlier code is a good example of using functions. If he just wants to get the code written because he's still learning then maybe don't worry so much about this right now if it makes it too difficult, but it's definitely something he would want to look at later. Once he has a working version then he can refactor it later to make it neater 👍

Great advice

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 5:48 pm
by roperclaret
pushpinpussy wrote:
Sun Mar 19, 2023 2:20 pm
don't take this the wrong way, but if your son needs his dad to come on a football message board asking for assistance in this matter then I'm not sure this is the career for him going forward.
Maybe he could fabricate a career in the legal profession 🙄

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 7:05 pm
by ynot
I'm learning python on Udemy, 100 days of code by Angela Yu. She explains everything so it's easy to understand and it only cost £12.

I'm using an Okdo Rock 4C+, getting hold of a Raspberry Pi is virtually impossible

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 7:58 pm
by Bosscat
pushpinpussy wrote:
Sun Mar 19, 2023 2:20 pm
don't take this the wrong way, but if your son needs his dad to come on a football message board asking for assistance in this matter then I'm not sure this is the career for him going forward.
And you complain about people picking on you ... Wow you really are a complete and utter ****

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 8:02 pm
by Croydon Claret
I'll try to share a basic construct tomorrow re how I would approach the problem

I don't use Python, or have experience of Raspberry Pi but I do teach programming in various other languages and the constructs are exactly the same

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 8:54 pm
by Marney&Mee
ClaretPete001 wrote:
Sun Mar 19, 2023 11:01 am
Depends what he is doing. This is a simple object oriented AI: Machine learning linear regression model using Pandas, matplotlib, sklearn etc.

He can have it but I'd struggle to help time wise.

It's starting off with the libraries, then opening a CSV and database, designing the model, fitting the model and finally allowing the model to learn from the data.

To be honest there are loads of sites for Python just nick some code and get it working.

import pyodbc
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
import seaborn as sns
import sys
import statistics

class Querydata:

def Connect_to_database():
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};Trusted_Connection=Yes;SERVER=IR- _____________;DATABASE=_________;TrustServerCertificate=Yes')
cursor = conn.cursor()
cursor.execute('SELECT * FROM D_DETAILS')
Records = cursor.fetchall()
return Records



#def Importdata():
# dfAchievement_data = pd.read_csv(r'C:\Users\________\project_data\_________.csv')
# print(dfAchievement_data)
# return dfAchievement_data

class Linear_regression:

#constructor

# Calling main function
# if __name__ == "__main__":
# main

def __init__(self):

# Data extraction

dfAchievement_data = Querydata.Connect_to_database()

#Data pre-processing
self.Check_for_nulls(dfAchievement_data)
self.Generatevisuals(dfAchievement_data)
dfAchievement_data = self.outlier_removal(dfAchievement_data)
print('dfAchievement_data', dfAchievement_data)
self.ReGeneratevisuals(dfAchievement_data)

# Identifiying the x and y variables
x, y = self.creatingxandyvariables(dfAchievement_data)
self.CheckforXandy(x, y)

# Training the data
x_train, x_test, y_train, y_test = self.splitdataset(x, y)
print('print before create', x_train, y_test)
Fitted_model = self.Createmodel(x_train, y_train)

# Creating the model
self.Prediction(Fitted_model,x_test, y_test)
coefficients, intercept = self.Assessment(Fitted_model)

# Implementing the model
Predicted_writing_score = self.calculate_charges(72, coefficients, intercept)
print("calculate_charges", Predicted_writing_score)

#Triangulaing the model
self.check_calculated_score(x,y)
sys.exit()

def Check_for_nulls(dfAchievement_data):
pd.isna(dfAchievement_data)
rpisnull = pd.isnull(dfAchievement_data)
print("Are there any nulls", rpisnull)
pd.DataFrame.isna(dfAchievement_data)


def Generatevisuals(dfAchievement_data):
dfAchievement_data.plot(x="gender", y="math score", kind="bar")
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def outlier_removal(dfAchievement_data):
Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("Reading upper limit: ", Reading_upper_limit)
print("Reading Lower Limit: ", Reading_lower_limit)
print("Writing upper limit: ", Writing_upper_limit)
print("Writing Lower Limit: ", Writing_lower_limit)
dfAchievement_data = dfAchievement_data[(dfAchievement_data['reading score'] > Reading_lower_limit) & (dfAchievement_data['reading score'] < Reading_upper_limit)]
dfAchievement_data = dfAchievement_data[(dfAchievement_data['writing score'] > Writing_lower_limit) & (dfAchievement_data['writing score'] < Writing_upper_limit)]

Reading_upper_limit = dfAchievement_data['reading score'].mean() + 3 * dfAchievement_data['reading score'].std()
Reading_lower_limit = dfAchievement_data['reading score'].mean() - 3 * dfAchievement_data['reading score'].std()
Writing_upper_limit = dfAchievement_data['writing score'].mean() + 3 * dfAchievement_data['writing score'].std()
Writing_lower_limit = dfAchievement_data['writing score'].mean() - 3 * dfAchievement_data['writing score'].std()
print("New Reading upper limit: ", Reading_upper_limit)
print("New Reading Lower Limit: ", Reading_lower_limit)
print("New Writing upper limit: ", Writing_upper_limit)
print("New Writing Lower Limit: ", Writing_lower_limit)
return dfAchievement_data


def ReGeneratevisuals(dfAchievement_data):
dfAchievement_data.plot(x="reading score", y="writing score", kind="bar")
# Instantiating a LinearRegression Model
sns.relplot(data=dfAchievement_data, x='reading score', y='writing score', hue='gender')
plt.show()
sns.pairplot(dfAchievement_data, hue='gender')
plt.show()


def creatingxandyvariables(dfAchievement_data):

x = dfAchievement_data['reading score']
y = dfAchievement_data['writing score']
print(x, y)
return x, y


def CheckforXandy(x, y):

print('Returned variable x', x)
print('Returned variable y', y)

def splitdataset(x, y):

x_train, x_test, y_train, y_test = train_test_split(
x, y, shuffle=True, train_size=0.3)
return x_train, x_test, y_train, y_test

def Createmodel(x_train, y_train):

print('in creatmodel func',x_train, y_train)
x_train = x_train.array.reshape(-1,1)
model = LinearRegression()
Fitted_model= model.fit(x_train, y_train)
return Fitted_model

def Prediction(model, x_test, y_test):

predictions = model.predict(x_test.array.reshape(-1,1))
r2 = r2_score(y_test, predictions)
rmse = mean_squared_error(y_test, predictions, squared=False)
print('The r2 is: ', r2)
print('The rmse is: ', rmse)


def Assessment(model):

coefficients = model.coef_
intercept = model.intercept_
return coefficients, intercept

def calculate_charges(Rdscore, coefficients, intercept):

print('Reading score', Rdscore)
return (Rdscore * coefficients) + intercept

def check_calculated_score(x,y):

print('Statistics mean x',statistics.mean(x))
print('Statistic mean y', statistics.mean(y))
Is that you LongTimeLurker?

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 8:56 pm
by Marney&Mee
Leyland Claret wrote:
Sun Mar 19, 2023 7:00 am
My lad has a project to complete for his A level in Design and Engineering. He is currently struggling to complete it and his tutors, how can say this…, aren’t being very helpful :evil: He has all the physical items but is struggling with the programming and coding side of things. Can you help :?: Hints, tips or actually speak/email with someone who has the knowledge to help. As you can imagine I am not particularly happy with the college and also working through the pandemic slightly hindered things…a lot!!
Has he tried turning it off, then on again?

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 9:06 pm
by Leyland Claret
Bosscat wrote:
Mon Mar 20, 2023 7:58 pm
And you complain about people picking on you ... Wow you really are a complete and utter ****
I don’t post on here much and I haven’t come across this person before but it was just a totally uncalled for response without knowing anything in depth about my sons situation with this. It is critical my lad gets this done and I thought I would try this media to seek assistance as he’s been utterly let down by his college tutors in my opinion. Thank you for taking the time to comment.

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 9:09 pm
by Leyland Claret
Marney&Mee wrote:
Mon Mar 20, 2023 8:56 pm
Has he tried turning it off, then on again?
To get it to turn on in the 1st place would be progress ;)

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 9:10 pm
by Leyland Claret
Croydon Claret wrote:
Mon Mar 20, 2023 8:02 pm
I'll try to share a basic construct tomorrow re how I would approach the problem

I don't use Python, or have experience of Raspberry Pi but I do teach programming in various other languages and the constructs are exactly the same
Thank you for your assistance in this.

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 9:15 pm
by Bosscat
Leyland Claret wrote:
Mon Mar 20, 2023 9:06 pm
I don’t post on here much and I haven’t come across this person before but it was just a totally uncalled for response without knowing anything in depth about my sons situation with this. It is critical my lad gets this done and I thought I would try this media to seek assistance as he’s been utterly let down by his college tutors in my opinion. Thank you for taking the time to comment.
He is a fantasist Leyland Claret with delusions of being a solicitor and multiple business owner ... His comment on here was well out of order as are his comments about people leaving football matches early ...

Re: Engineering project - Python with Raspberry Pi

Posted: Mon Mar 20, 2023 11:41 pm
by ClaretPete001
There has been some good advice on here from Croydon and others. The site above objects to ad blocker but this one just gives you the code. For the record I know nothing about coding a Pi or any other hardware:

https://realpython.com/python-raspberry ... 20get%2Dgo.

This one has a list of Pi classes: https://gpiozero.readthedocs.io/en/stable/

And all you do is call them and create instances of the class

button = Button(4, hold_time=2.5)
button.when_held = button_held

So, if you want to code a button press - I have no idea why you would but if you did just add our class libraries to the code

#Python libraries for Pi
from gpiozero import Button
from signal import pause

...then create a set of functions

#create a set of functions
def button_pressed():
print("Button was pressed")

def button_held():
print("Button was held")

def button_released():
print("Button was released")

And then call the classes creating instances of the classes. For example, button 4 pressed for 2.5 seconds would be called like this:

button = Button(4, hold_time=2.5)

So, when button 4 is pressed for 2.5 seconds the Pi will call whatever code you son has written:

So add the following function call to this function:

def button_held():
print("Button was held")
Self_destruct()

And your son just complete the project..shaken but not stirred Q!

#Pi self destructs
Def Self_destruct
print("Button was held and the Pi will now self destruct")
______Insert Code___________

It's all on the site I added the nerdy humour for effect.

And then add in Croydon's advice on programming technique and your son should have a half decent answer.

Re: Engineering project - Python with Raspberry Pi

Posted: Tue Mar 21, 2023 1:27 pm
by Croydon Claret
I came up with a basic template for him. It's just a starter and will no doubt need change as things become more known but it might give him an idea of how to break the challenge down into its constituent parts and not get bogged down trying to write all his code in one function.

Caveat - I don't program with Python so hope there's no syntax errors. Haven't fleshed out any of the logic, but given indicators for what each section should do.

I wasn't sure what the exact rules were for "read the keypad" so had to cut my template short as I'm not sure what the inputted data represented. I added some comments in the example template to show where I'm confused

I understand that the numbers later need to be displayed in hh:mm format but wasn't sure what the numbers coming in represent

- Do the inputted numbers represented an hh:mm time format e.g. "12:59" rather than an amount of seconds?
- Is he expected to wait until a set number of buttons have been pressed before processing the input? if so then that leaves you wide open to bugs if converting to hh:mm and the user pressed 4 large numbers e.g. "12", "11", "10" and "12", which you then can't convert to a time

If you're not waiting for a set number of button presses then at some point you have to tell the program that you're done listening for button presses so that the rest of the logic can flow. Wasn't sure if there was further info about what is deemed to be sufficient button presses to continue and whether the inputted numbers should be deemed as an amount of seconds or an exact time in hours and minutes?


back to the code...

At which point is he stuck at the moment? Has he managed to connect to the keyboard and convert the button presses into numbers(regardless of what those numbers actually mean as mentioned above)?


TEMPLATE
# Read the number from the keyboard
userInput = inputFromKeyboard()

# You might need a date/time formatting function here depending on how you're processing the inputted numbers. See questions in "inputFromKeyboard()" method
# userInput = formatToDataTime(userInput)

# Connect to the LED Panel and set the defaults
initialiseLEDPanel()

# Display the number on the LED panel
timeCountdown(userInput)

# Light up the LED dots
illuminateLEDDots())

def inputFromKeyboard():
# Add logic here to capture the keyboard numbers

# (Not sure what the requirements or expectations are here.)
# See questions in thread


def timeCountdown(timeToDisplay):
# Set up a "while" loop that counts down the time and calls the display function every second

# Start of "while" loop (while loop continues until time is at or equal to zero)
# Call the display function passing in the current time
displayOnLED(timeToDisplay)

# Add logic to subtract one second from the time

# End of "while" loop

def initialiseLEDPanel():
# Add logic to initialise the LED panel
# https://circuitdigest.com/microcontroll ... lay-module

def displayOnLED(timeToDisplay):
# format the timeToDisplay parameter into 4 numbers(preferably by calling a dedicated function) and illuminate the LEDs
# https://circuitdigest.com/microcontroll ... lay-module

def illuminateLEDDots():
# Add logic to illuminate the LED dots

Re: Engineering project - Python with Raspberry Pi

Posted: Tue Mar 21, 2023 8:44 pm
by Leon_C
Panthro wrote:
Sun Mar 19, 2023 8:56 am
Codecademy is a good place to start learning the fundamentals of programming using various languages, here is the Python link: -https://www.codecademy.com/learn/learn-python-3
I've got a little experience setting up a Raspberry Pi (albeit, at OS level and running a Node.js server.) I've done a bit of work with Arduino - which is probably more suited to the task based on what I've read.
I've not actually developed in Python, but I've done a bit in JS and historically ASP/C/.NET.

I've scanned this post, and I think the missing information is the actual level of knowledge of the student himself. If he's at level 0, without any Python or programming capability, then to be honest, most of the well intentioned posts above will just go over his head.

There are 2 fundamental pre-requisites here, and we don't know if they're in place.
a) a reasonable level of Python - Codecademy would be a great place to start
b) an understanding of the Raspberry Pi, in particular addressing hardware as per the brief


Without (a) then the exercise will fail. The student must learn how to write basic routines, loops and I imagine, for the task, manipulate arrays/objects. (Supplying any kind of templates or sample code, without this knowledge, is going to be rather pointless unfortunately. As helpful and well intentioned the posters are.)

I've enjoyed reading Uptheclarets X Stackexchange tonight 😅

Re: Engineering project - Python with Raspberry Pi

Posted: Tue Mar 21, 2023 8:56 pm
by Leyland Claret
Croydon Claret wrote:
Tue Mar 21, 2023 1:27 pm
I came up with a basic template for him. It's just a starter and will no doubt need change as things become more known but it might give him an idea of how to break the challenge down into its constituent parts and not get bogged down trying to write all his code in one function.

Caveat - I don't program with Python so hope there's no syntax errors. Haven't fleshed out any of the logic, but given indicators for what each section should do.

I wasn't sure what the exact rules were for "read the keypad" so had to cut my template short as I'm not sure what the inputted data represented. I added some comments in the example template to show where I'm confused

I understand that the numbers later need to be displayed in hh:mm format but wasn't sure what the numbers coming in represent

- Do the inputted numbers represented an hh:mm time format e.g. "12:59" rather than an amount of seconds?
- Is he expected to wait until a set number of buttons have been pressed before processing the input? if so then that leaves you wide open to bugs if converting to hh:mm and the user pressed 4 large numbers e.g. "12", "11", "10" and "12", which you then can't convert to a time

If you're not waiting for a set number of button presses then at some point you have to tell the program that you're done listening for button presses so that the rest of the logic can flow. Wasn't sure if there was further info about what is deemed to be sufficient button presses to continue and whether the inputted numbers should be deemed as an amount of seconds or an exact time in hours and minutes?


back to the code...

At which point is he stuck at the moment? Has he managed to connect to the keyboard and convert the button presses into numbers(regardless of what those numbers actually mean as mentioned above)?


TEMPLATE
# Read the number from the keyboard
userInput = inputFromKeyboard()

# You might need a date/time formatting function here depending on how you're processing the inputted numbers. See questions in "inputFromKeyboard()" method
# userInput = formatToDataTime(userInput)

# Connect to the LED Panel and set the defaults
initialiseLEDPanel()

# Display the number on the LED panel
timeCountdown(userInput)

# Light up the LED dots
illuminateLEDDots())

def inputFromKeyboard():
# Add logic here to capture the keyboard numbers

# (Not sure what the requirements or expectations are here.)
# See questions in thread


def timeCountdown(timeToDisplay):
# Set up a "while" loop that counts down the time and calls the display function every second

# Start of "while" loop (while loop continues until time is at or equal to zero)
# Call the display function passing in the current time
displayOnLED(timeToDisplay)

# Add logic to subtract one second from the time

# End of "while" loop

def initialiseLEDPanel():
# Add logic to initialise the LED panel
# https://circuitdigest.com/microcontroll ... lay-module

def displayOnLED(timeToDisplay):
# format the timeToDisplay parameter into 4 numbers(preferably by calling a dedicated function) and illuminate the LEDs
# https://circuitdigest.com/microcontroll ... lay-module

def illuminateLEDDots():
# Add logic to illuminate the LED dots
Thank you for your amazing help with this to you and all the other (minus 1 plank) people who have contributed. He has digested all the information given on here and from other sources and he has come up with a solution! He has scaled the project down a little as it was a little too in-depth for a complete novice. So now he has a countdown timer that concludes with an LED lighting up and an audible buzzer. He choose this project originally because he would have aced it using circuits but his ‘tutors’ decided it needed to be run through programming and coding which hadn’t been covered. When he questioned this his tutors gave him examples of coding on YouTube to watch and to crack on! He’s not had the best of times in his college years but fingers crossed he gets out with the grades that he requires. He loves his engineering and is hoping to bag himself an apprenticeship. Thank you all again for your fantastic input.

Re: Engineering project - Python with Raspberry Pi

Posted: Tue Mar 21, 2023 9:39 pm
by Leon_C
Best of luck to him, Leyland!