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.
Dependencies: mbed
main.cpp@1:98bd0c4131d8, 2022-06-28 (annotated)
- Committer:
- samh25
- Date:
- Tue Jun 28 16:07:52 2022 +0000
- Revision:
- 1:98bd0c4131d8
- Parent:
- 0:fe11ae12c823
2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samh25 | 0:fe11ae12c823 | 1 | // TBRe-AI FRONT MBED |
samh25 | 0:fe11ae12c823 | 2 | // MONITORS STEERING ANGLE AND WHEEL SPEED |
samh25 | 0:fe11ae12c823 | 3 | // AND SENDS THEM OVER CAN BUS |
samh25 | 0:fe11ae12c823 | 4 | // WRITTEN BY SEB HALL ON 29/06/2022 |
samh25 | 0:fe11ae12c823 | 5 | |
samh25 | 0:fe11ae12c823 | 6 | #include "mbed.h" |
samh25 | 0:fe11ae12c823 | 7 | |
samh25 | 0:fe11ae12c823 | 8 | const PinName writePin = p29; |
samh25 | 0:fe11ae12c823 | 9 | const PinName readPin = p30; |
samh25 | 0:fe11ae12c823 | 10 | const int busFrequency = 500000; |
samh25 | 0:fe11ae12c823 | 11 | const unsigned int steeringMessageID = 100; |
samh25 | 0:fe11ae12c823 | 12 | |
samh25 | 0:fe11ae12c823 | 13 | DigitalOut led1(LED1); |
samh25 | 0:fe11ae12c823 | 14 | DigitalOut led2(LED2); |
samh25 | 0:fe11ae12c823 | 15 | Serial pc(USBTX, USBRX); |
samh25 | 0:fe11ae12c823 | 16 | AnalogIn steeringIn(p15); |
samh25 | 0:fe11ae12c823 | 17 | |
samh25 | 0:fe11ae12c823 | 18 | union { |
samh25 | 0:fe11ae12c823 | 19 | double formattedData; |
samh25 | 0:fe11ae12c823 | 20 | unsigned char rawData[8]; |
samh25 | 0:fe11ae12c823 | 21 | } CANFormatted; |
samh25 | 0:fe11ae12c823 | 22 | |
samh25 | 0:fe11ae12c823 | 23 | CAN CANBus(readPin, writePin, busFrequency); |
samh25 | 0:fe11ae12c823 | 24 | |
samh25 | 0:fe11ae12c823 | 25 | void sendCANMessage(double data, unsigned int id) { |
samh25 | 0:fe11ae12c823 | 26 | |
samh25 | 0:fe11ae12c823 | 27 | CANMessage message; |
samh25 | 0:fe11ae12c823 | 28 | |
samh25 | 0:fe11ae12c823 | 29 | CANFormatted.formattedData = data; |
samh25 | 0:fe11ae12c823 | 30 | |
samh25 | 0:fe11ae12c823 | 31 | for (int i = 0; i < 8; i++) { |
samh25 | 0:fe11ae12c823 | 32 | message.data[i] = CANFormatted.rawData[i]; |
samh25 | 0:fe11ae12c823 | 33 | } |
samh25 | 0:fe11ae12c823 | 34 | |
samh25 | 0:fe11ae12c823 | 35 | message.id = id; |
samh25 | 0:fe11ae12c823 | 36 | |
samh25 | 0:fe11ae12c823 | 37 | CANBus.write(message); |
samh25 | 0:fe11ae12c823 | 38 | |
samh25 | 0:fe11ae12c823 | 39 | } |
samh25 | 0:fe11ae12c823 | 40 | |
samh25 | 0:fe11ae12c823 | 41 | int main() { |
samh25 | 0:fe11ae12c823 | 42 | pc.printf("FRONT STARTED\n"); |
samh25 | 0:fe11ae12c823 | 43 | led1 = !led1; |
samh25 | 0:fe11ae12c823 | 44 | while(1) { |
samh25 | 1:98bd0c4131d8 | 45 | float num = (float) steeringIn.read(); |
samh25 | 0:fe11ae12c823 | 46 | sendCANMessage(num, steeringMessageID); |
samh25 | 0:fe11ae12c823 | 47 | led1 = !led1; |
samh25 | 0:fe11ae12c823 | 48 | led2 = !led2; |
samh25 | 0:fe11ae12c823 | 49 | } |
samh25 | 0:fe11ae12c823 | 50 | } |