Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 111:f11575e7c79b, committed 2022-04-07
- Comitter:
- voltxd
- Date:
- Thu Apr 07 12:30:01 2022 +0000
- Parent:
- 110:a6d1d3525014
- Child:
- 112:478ae92cb106
- Commit message:
- .;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| protocol.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Apr 07 12:13:04 2022 +0000
+++ b/main.cpp Thu Apr 07 12:30:01 2022 +0000
@@ -5,13 +5,8 @@
#include "mbed.h"
#include "platform/mbed_thread.h"
-#include <SPISlave.h>
#include "protocol.h"
-
-// Blinking rate in milliseconds
-#define BLINKING_RATE_MS 2000
-
//Define SPI
#define SPI3_MOSI D11
#define SPI3_MISO D12
@@ -22,67 +17,52 @@
#define PWM_PROP D10
#define PWM_DIR D9
-//Déclarations PWM
+//Declaration PWM outputs
PwmOut propulsion(PWM_PROP);
PwmOut direction(PWM_DIR);
-//Déclarations Liaisons
+//Declaration Links
static BufferedSerial serial_port(USBTX, USBRX,115200);
SPISlave device(SPI3_MOSI, SPI3_MISO, SPI3_SCLK, SPI3_CS); // mosi, miso, sclk, ssel
int main()
{
+ //Declaration PWM variables
uint32_t pulsewidth_direction = 1100;
uint32_t pulsewidth_propulsion = 1500;
- char texte[32];
+ //Test Serial port
+ serial_port.write("Test\n\r",strlen("Test\n\r"));
- sprintf(texte,"Test\n\r");
- serial_port.write(texte,strlen(texte));
-
+ //Init. propulsion PWM
propulsion.period_us(20000);
propulsion.pulsewidth_us(pulsewidth_propulsion);
thread_sleep_for(2000);
+ //Init. Direction PWM
direction.period_us(20000);
direction.pulsewidth_us(pulsewidth_direction);
+ //Init. SPI Link
device.format(8);
- while (true) {
+ //Infinite loop
+ while(1)
+ {
+ //If SPI received a char, decode it then reply bullshit.
if(device.receive())
{
- //decodeMessage((char)device.read());
+ decodeMessage((char)device.read());
+ device.reply(0b10101010);
}
+
+ //If decoding has ended, get and changes PWM values.
if(isDataAvailable())
{
getVerifiedPWMValues(&pulsewidth_propulsion, &pulsewidth_direction);
propulsion.pulsewidth_us(pulsewidth_propulsion);
direction.pulsewidth_us(pulsewidth_direction);
- }
-
- /*
-
- propulsion.pulsewidth_us(1450);
- thread_sleep_for(BLINKING_RATE_MS);
- led = !led;
- propulsion.pulsewidth_us(1400);
- thread_sleep_for(BLINKING_RATE_MS);
- led = !led;
- propulsion.pulsewidth_us(1500);
- thread_sleep_for(BLINKING_RATE_MS);
- led = !led;
- propulsion.pulsewidth_us(1550);
- thread_sleep_for(BLINKING_RATE_MS);
- led = !led;
- propulsion.pulsewidth_us(1600);
- thread_sleep_for(BLINKING_RATE_MS);
- led = !led;
- propulsion.pulsewidth_us(1800);
- thread_sleep_for(BLINKING_RATE_MS);
- */
-
-
+ }
}
}
--- a/protocol.cpp Thu Apr 07 12:13:04 2022 +0000
+++ b/protocol.cpp Thu Apr 07 12:30:01 2022 +0000
@@ -2,6 +2,7 @@
char newDataAvailable = 0;
+//Bytewise XOR
char calculateChecksum(uint32_t propulsion, uint32_t direction)
{
char checksum = 0xff;
@@ -21,6 +22,7 @@
uint32_t verifiedPropulsion = 1500;
uint32_t verifiedDirection = 1150;
+//Decode the bytes received according to their order of reception.
void decodeMessage(char c)
{
switch(receiveState)
@@ -61,7 +63,7 @@
//Checksum
if (c == calculateChecksum(receivedPropulsion, receivedDirection))
{
- //Le message recu est correct, on peut exploiter ses données
+ //The frame is correct, we can use the values retrieved.
verifiedPropulsion = receivedPropulsion;
verifiedDirection = receivedDirection;
newDataAvailable = 1;
@@ -75,6 +77,7 @@
}
}
+//Return the values decoded
void getVerifiedPWMValues(uint32_t *pwmPropulsion, uint32_t *pwmDirection)
{
*pwmPropulsion = verifiedPropulsion;
@@ -82,6 +85,7 @@
newDataAvailable = 0;
}
+//Return a value telling if new informations are available.
char isDataAvailable()
{
return newDataAvailable;