Seguilinea KP Taratura Ultrasuoni di Paolo Giovanni
Dependencies: HCSR04 X_NUCLEO_IHM12A1 mbed
Fork of Ilfunzionante by
Revision 0:9bd4730782f9, committed 2018-02-17
- Comitter:
- PaoloGiovanni99
- Date:
- Sat Feb 17 11:05:43 2018 +0000
- Child:
- 1:0226a8d3c0d3
- Commit message:
- seguilinea funzionante Marcianise
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04.lib Sat Feb 17 11:05:43 2018 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/prabhuvd/code/HCSR04/#71da0dbf4400
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PLT_DCmotorShield_IHM12A1/MotorShieldIHM12A1.cpp Sat Feb 17 11:05:43 2018 +0000
@@ -0,0 +1,144 @@
+#include "MotorShieldIHM12A1.h"
+#include "mbed.h"
+
+
+/* Variables -----------------------------------------------------------------*/
+
+/* Initialization parameters of the motor connected to the expansion board. */
+ Stspin240_250_Init_t initDeviceParameters = {
+ 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */
+ 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */
+ 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */
+ 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */
+ TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */
+ };
+
+/* Motor Control Component. */
+STSPIN240_250 *motor;
+
+/* Functions -----------------------------------------------------------------*/
+void myErrorHandler(uint16_t error)
+{
+ /* Printing to the console. */
+ printf("Error %d detected\r\n\n", error);
+
+ /* Infinite loop */
+ while(1)
+ {
+ }
+}
+
+void myFlagIRQHandler(void)
+{
+
+ printf(" WARNING: \"FLAG\" interrupt triggered.\r\n");
+
+ /* Get the state of bridge A */
+ uint16_t bridgeState = motor->GetBridgeStatus(0);
+
+ if (bridgeState == 0)
+ {
+ if (motor->GetDeviceState(0) != INACTIVE)
+ {
+ /* Bridges were disabled due to overcurrent or over temperature */
+ /* When motor was running */
+ myErrorHandler(0XBAD0);
+ }
+ }
+}
+
+MotorShieldIHM12A1::MotorShieldIHM12A1()
+{
+
+//----- Initialization
+
+ /* Initializing Motor Control Component. */
+ #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8)
+ motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A2);
+ #elif (defined TARGET_NUCLEO_L152RE)
+ motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A3);
+ #else
+ motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0);
+ #endif
+ if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE);
+
+ /* Set dual bridge enabled as two motors are used*/
+ motor->SetDualFullBridgeConfig(1);
+
+ /* Attaching and enabling an interrupt handler. */
+ motor->AttachFlagIRQ(&myFlagIRQHandler);
+ motor->EnableFlagIRQ();
+
+ /* Attaching an error handler */
+ motor->AttachErrorHandler(&myErrorHandler);
+
+ /* Set PWM Frequency of Ref to 15000 Hz */
+ motor->SetRefPwmFreq(0, 15000);
+
+ /* Set PWM duty cycle of Ref to 60% */
+ motor->SetRefPwmDc(0, 60);
+
+ /* Set PWM Frequency of bridge A inputs to 10000 Hz */
+ motor->SetBridgeInputPwmFreq(0,10000);
+
+ /* Set PWM Frequency of bridge B inputs to 10000 Hz */
+ motor->SetBridgeInputPwmFreq(1,10000);
+
+}
+
+void MotorShieldIHM12A1::motorSpeed(unsigned int ID, int8_t speed)
+{
+ if (speed > 100) speed = 100;
+ else if (speed < -100) speed = -100;
+
+ if (speed>0)
+ {
+ motor->SetSpeed(ID,speed);
+ motor->Run(ID, BDCMotor::FWD);
+ }
+ else if (speed<0)
+ {
+ motor->SetSpeed(ID,-speed);
+ motor->Run(ID, BDCMotor::BWD);
+ }
+ else
+ {
+ motor->HardStop(ID);
+ }
+}
+
+void MotorShieldIHM12A1::motorStop(unsigned int ID)
+{
+ motor->HardHiZ(ID);
+}
+
+void MotorShieldIHM12A1::motorReset()
+{
+ motor->Reset();
+}
+
+void MotorShieldIHM12A1::turn(int8_t direction, int8_t speed)
+{
+ bool left = false;
+ float speedMod;
+
+ if (direction < 0)
+ {
+ left = true;
+ direction = -direction;
+ }
+
+ speedMod = ((float)speed/100)*(direction-50)*(-2);
+
+ if (left)
+ {
+
+ motorSpeed(1,speedMod);
+ motorSpeed(0,speed);
+ }
+ else
+ {
+ motorSpeed(0,speedMod);
+ motorSpeed(1,speed);
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PLT_DCmotorShield_IHM12A1/MotorShieldIHM12A1.h Sat Feb 17 11:05:43 2018 +0000
@@ -0,0 +1,28 @@
+/****************************************************
+* FAST PROTOTYPING WITH NUCLEO *
+* Author: Francesco Caiazzo *
+* Organization: Perlatecnica no-profit organization *
+*****************************************************/
+
+
+#ifndef MOTOR_SHIELDIHM12A1_H
+#define MOTOR_SHIELDIHM12A1_H
+
+#include "stspin240_250_class.h"
+
+class MotorShieldIHM12A1 {
+
+public:
+
+// Crea un'istanza di MotorShield
+ MotorShieldIHM12A1();
+ void motorSpeed(unsigned int ID, int8_t speed);
+ void motorStop(unsigned int ID);
+ void motorReset(void);
+ void turn(int8_t direction, int8_t speed);
+
+private:
+ Stspin240_250_Init_t initDeviceParameters;
+ STSPIN240_250 *motor;
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X-NUCLEO-IHM12A1.lib Sat Feb 17 11:05:43 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/X_NUCLEO_IHM12A1/#232e0c730f59
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Feb 17 11:05:43 2018 +0000
@@ -0,0 +1,93 @@
+#include "mbed.h"
+#include "MotorShieldIHM12A1.h"
+#include "hcsr04.h"
+
+#define SPEED 50
+float DIR;
+float mediaB = 0;
+float mediaN = 0 ;
+float MediaT = 0;
+
+#define Kp 50
+#define offset 0
+#define C 5
+
+HCSR04 sensor(D10, D11);
+// Serial
+Serial pc(USBTX, USBRX);
+//PwmOut mypwm(A0);
+
+MotorShieldIHM12A1 ms;
+AnalogIn lineaD(A5);
+DigitalIn pulsante(PC_13);
+
+int main()
+{
+ pc.baud(9600);
+ pc.printf("Here I am!\r\n");
+ //ms.MotorStart();
+ printf("start \r\n");
+ // Main loop
+ printf ("Val \r\n ");
+
+
+
+
+
+ while (pulsante)
+ {
+ wait (0.1);
+ }
+ wait(0.1);
+
+ for (int i=0; i<5; i++)
+ {
+ mediaB += lineaD.read();
+ wait(0.1);
+ }
+
+ mediaB /= C;
+ printf ("%f \n\r", mediaB);
+ printf("Bianco \r\n");
+
+ while (pulsante)
+ {
+ wait (0.1);
+ }
+ wait(0.1);
+
+ for (int i=0; i<C; i++)
+ {
+ mediaN += lineaD.read();
+ wait(0.1);
+ }
+ mediaN /= C;
+ printf ("%f \n\r", mediaB);
+ printf("Nero \r\n");
+ MediaT =(mediaB+mediaN) / 2;
+
+ while (pulsante) {
+ wait (0.1);
+ }
+ wait(0.1);
+ while(true)
+ {
+ // Avvia un impulso della durata di 10us sul pin di trigger
+ sensor.start();
+
+ // Aspetta prima della prossima lettura
+ wait_ms(300);
+
+ // Stampa sulla seriale la misura della distanza in cm
+ pc.printf("%dcm\r\n", sensor.get_dist_cm());
+
+ if (sensor.get_dist_cm() > 20) {
+
+ ms.turn((lineaD.read()-MediaT)*Kp,40);
+ }
+ else {
+ ms.turn(0,0);
+ }
+
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Feb 17 11:05:43 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f \ No newline at end of file
