Skip to content

Commit

Permalink
Added CM conversion and mbed_app.json for prinf with floats
Browse files Browse the repository at this point in the history
  • Loading branch information
PAOK-2001 committed May 18, 2022
1 parent 7438221 commit 929dfba
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .theia/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0988{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": []
}
42 changes: 35 additions & 7 deletions CleanIt-SC/Headers/proximity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

void adc_init(void);
void proximity_init(void);
int proximity_read_average(int sensorValue, int meassurments);
double proximity_read_average(int sensorValue, int meassurments);

//adc_init()
// Initialized clock for ADC0 and configures necessary register.
Expand All @@ -31,19 +31,47 @@ void proximity_init(void){
//proximity_read()
// @param sensorChannel: int corresponding to ADC channel. Should be 8 or 9.
// @param meassurments: measurments to average
// @returns int value corresponding to the sensor value
int proximity_read_average(int sensorChannel, int meassurments){
// @returns float in cm
double proximity_read_average(int sensorChannel, int meassurments){
int rawValue,sum;
int valueCm;
double valueCm;
sum = 0;
for (int i = 0; i < meassurments; i++){
ADC0->SC1[0] = sensorChannel;
while(!(ADC0->SC1[0] & 0x80)) { } /* wait for COCO */
sum = sum + ADC0->R[0];
delayMs(15);
delayMs(50);
}
rawValue = sum/meassurments;
return rawValue;
valueCm = 66.3 -0.0507*rawValue + 0.0000163*pow(rawValue,2) -0.00000000188*pow(rawValue,3);
return valueCm;
}

#endif
#endif


/* Driver code
#include "mbed.h"
#include <MKL25Z4.h>
#include"Headers/proximity.h"
#include "Headers/delayTPM0.h"
#include<cstdio>
// main() runs in its own thread in the OS
int main(){
printf("Program Begin\n");
tpm_init();
proximity_init();
double leftSensor, rightSensor;
double test = 9.64;
while (true) {
leftSensor = proximity_read_average(8,20);
printf("%.9f \n", leftSensor);
tpm_delayMs(500);
// rightSensor = proximity_read_average(9, 20);
// printf("Right Sensor: %d\n\n", rightSensor);
// tpm_delayMs(250);
}
}
*/
8 changes: 0 additions & 8 deletions CleanIt-SC/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@

// main() runs in its own thread in the OS
int main(){
printf("Program Begin\n");
tpm_init();
proximity_init();
int leftSensor, rightSensor;
while (true) {
leftSensor = proximity_read_average(8,20);
printf("%d\n", leftSensor);
tpm_delayMs(1000);

}
}

11 changes: 11 additions & 0 deletions CleanIt-SC/mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"requires": ["bare-metal"],
"target_overrides": {
"*": {
"target.c_lib": "small",
"target.printf_lib": "minimal-printf",
"platform.minimal-printf-enable-floating-point": true,
"platform.stdio-minimal-console-only": true
}
}
}

0 comments on commit 929dfba

Please sign in to comment.