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: SERVO_PPM mbed Servo
main.cpp@0:032a5658acdc, 2021-03-16 (annotated)
- Committer:
- MarcusEA
- Date:
- Tue Mar 16 04:00:51 2021 +0000
- Revision:
- 0:032a5658acdc
- Child:
- 1:b3151b6fe36b
Base Joystick CANBus to PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarcusEA | 0:032a5658acdc | 1 | // Keep sweeping servo from left to right |
MarcusEA | 0:032a5658acdc | 2 | #include "mbed.h" |
MarcusEA | 0:032a5658acdc | 3 | #include "Servo.h" |
MarcusEA | 0:032a5658acdc | 4 | |
MarcusEA | 0:032a5658acdc | 5 | #define JOYSTICK_ID 0x0cfdd633 //can also be 0x0CFDD633 |
MarcusEA | 0:032a5658acdc | 6 | #define CAN_TD D2 |
MarcusEA | 0:032a5658acdc | 7 | #define CAN_RD D10 |
MarcusEA | 0:032a5658acdc | 8 | |
MarcusEA | 0:032a5658acdc | 9 | //PWM Outputs |
MarcusEA | 0:032a5658acdc | 10 | Servo leftESC(D3); |
MarcusEA | 0:032a5658acdc | 11 | Servo rightESC(A6); |
MarcusEA | 0:032a5658acdc | 12 | |
MarcusEA | 0:032a5658acdc | 13 | int leftDuty = 1500; |
MarcusEA | 0:032a5658acdc | 14 | int rightDuty = 1500; |
MarcusEA | 0:032a5658acdc | 15 | |
MarcusEA | 0:032a5658acdc | 16 | bool deadMan = false; |
MarcusEA | 0:032a5658acdc | 17 | |
MarcusEA | 0:032a5658acdc | 18 | //CANBus |
MarcusEA | 0:032a5658acdc | 19 | CAN can(CAN_RD, CAN_TD); |
MarcusEA | 0:032a5658acdc | 20 | |
MarcusEA | 0:032a5658acdc | 21 | //**FOR SERIAL OUTPUT USING PUTTY using 64-bit avaialble from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html |
MarcusEA | 0:032a5658acdc | 22 | Serial pc(SERIAL_TX, SERIAL_RX); |
MarcusEA | 0:032a5658acdc | 23 | |
MarcusEA | 0:032a5658acdc | 24 | Ticker ticker; |
MarcusEA | 0:032a5658acdc | 25 | |
MarcusEA | 0:032a5658acdc | 26 | //Function to read the CANbus and decode the data |
MarcusEA | 0:032a5658acdc | 27 | void readCAN(void) { |
MarcusEA | 0:032a5658acdc | 28 | CANMessage msg; |
MarcusEA | 0:032a5658acdc | 29 | if(can.read(msg)) { |
MarcusEA | 0:032a5658acdc | 30 | |
MarcusEA | 0:032a5658acdc | 31 | if((int)msg.id == JOYSTICK_ID) { |
MarcusEA | 0:032a5658acdc | 32 | // pc.printf("RX CANPAcket\r\n"); |
MarcusEA | 0:032a5658acdc | 33 | int rightStatus = (int)(msg.data[0]>>4)%4; |
MarcusEA | 0:032a5658acdc | 34 | int leftStatus = (int)(msg.data[0]>>2)%4; |
MarcusEA | 0:032a5658acdc | 35 | int centerXStatus = (int)msg.data[0]%4; |
MarcusEA | 0:032a5658acdc | 36 | |
MarcusEA | 0:032a5658acdc | 37 | int xAxisRaw = (int)(msg.data[0]>>6) + (msg.data[1]<<2); |
MarcusEA | 0:032a5658acdc | 38 | |
MarcusEA | 0:032a5658acdc | 39 | int forwardStatus = (int)(msg.data[2]>>4)%4; |
MarcusEA | 0:032a5658acdc | 40 | int backwardStatus = (int)(msg.data[2]>>2)%4; |
MarcusEA | 0:032a5658acdc | 41 | int centerYStatus = (int)msg.data[2]%4; |
MarcusEA | 0:032a5658acdc | 42 | |
MarcusEA | 0:032a5658acdc | 43 | int yAxisRaw = (int)(msg.data[2]>>6) + (msg.data[3]<<2); |
MarcusEA | 0:032a5658acdc | 44 | |
MarcusEA | 0:032a5658acdc | 45 | //note button 1 = top left, button 2 = top right |
MarcusEA | 0:032a5658acdc | 46 | int button1 = (int)(msg.data[5]>>6)%4; |
MarcusEA | 0:032a5658acdc | 47 | int button2 = (int)(msg.data[5]>>4)%4; |
MarcusEA | 0:032a5658acdc | 48 | int button3 = (int)(msg.data[5]>>2)%4; |
MarcusEA | 0:032a5658acdc | 49 | int button4 = (int)msg.data[5]%4; |
MarcusEA | 0:032a5658acdc | 50 | |
MarcusEA | 0:032a5658acdc | 51 | //note button 8 = dead-man switch |
MarcusEA | 0:032a5658acdc | 52 | int button5 = (int)(msg.data[6]>>6)%4; |
MarcusEA | 0:032a5658acdc | 53 | int button6 = (int)(msg.data[6]>>4)%4; |
MarcusEA | 0:032a5658acdc | 54 | int button7 = (int)(msg.data[6]>>2)%4; |
MarcusEA | 0:032a5658acdc | 55 | int button8 = (int)msg.data[6]%4; |
MarcusEA | 0:032a5658acdc | 56 | |
MarcusEA | 0:032a5658acdc | 57 | int button9 = (int)(msg.data[7]>>6)%4; |
MarcusEA | 0:032a5658acdc | 58 | int button10 = (int)(msg.data[7]>>4)%4; |
MarcusEA | 0:032a5658acdc | 59 | int button11 = (int)(msg.data[7]>>2)%4; |
MarcusEA | 0:032a5658acdc | 60 | int button12 = (int)msg.data[7]%4; |
MarcusEA | 0:032a5658acdc | 61 | |
MarcusEA | 0:032a5658acdc | 62 | //pc.printf("RightStatus:%d,LeftStatus:%d,CenterXStatus:%d,XAxisRaw:%d\n\r", rightStatus, leftStatus, centerXStatus, xAxisRaw); |
MarcusEA | 0:032a5658acdc | 63 | //pc.printf("ForwardStatus:%d,BackwardStatus:%d,CenterYStatus:%d,YAxisRaw:%d\n\r", forwardStatus, backwardStatus, centerYStatus, yAxisRaw); |
MarcusEA | 0:032a5658acdc | 64 | |
MarcusEA | 0:032a5658acdc | 65 | //pc.printf("Button1:%d,Button2:%d,Button3:%d,Button4:%d\n\r", button1, button2, button3, button4); |
MarcusEA | 0:032a5658acdc | 66 | //pc.printf("Button5:%d,Button6:%d,Button7:%d,Button8:%d\n\r", button5, button6, button7, button8); |
MarcusEA | 0:032a5658acdc | 67 | //pc.printf("Button1:%d,Button2:%d,Button3:%d,Button3:%d\n\r", button1, button2, button3, button4); |
MarcusEA | 0:032a5658acdc | 68 | |
MarcusEA | 0:032a5658acdc | 69 | deadMan = button8; |
MarcusEA | 0:032a5658acdc | 70 | //modifying duties |
MarcusEA | 0:032a5658acdc | 71 | if(!deadMan) { |
MarcusEA | 0:032a5658acdc | 72 | leftDuty = 1500; |
MarcusEA | 0:032a5658acdc | 73 | rightDuty = 1500; |
MarcusEA | 0:032a5658acdc | 74 | pc.printf("DeadMan:OFF, LeftDuty:%d,RightDuty:%d\n\r", leftDuty, rightDuty); |
MarcusEA | 0:032a5658acdc | 75 | } |
MarcusEA | 0:032a5658acdc | 76 | else { |
MarcusEA | 0:032a5658acdc | 77 | int signedXAxis = xAxisRaw; |
MarcusEA | 0:032a5658acdc | 78 | if(leftStatus==1) { |
MarcusEA | 0:032a5658acdc | 79 | signedXAxis = -1*signedXAxis; |
MarcusEA | 0:032a5658acdc | 80 | } |
MarcusEA | 0:032a5658acdc | 81 | |
MarcusEA | 0:032a5658acdc | 82 | int signedYAxis = yAxisRaw; |
MarcusEA | 0:032a5658acdc | 83 | if(backwardStatus==1) { |
MarcusEA | 0:032a5658acdc | 84 | signedYAxis = -1*signedYAxis; |
MarcusEA | 0:032a5658acdc | 85 | } |
MarcusEA | 0:032a5658acdc | 86 | |
MarcusEA | 0:032a5658acdc | 87 | //scale between 1000-2000 |
MarcusEA | 0:032a5658acdc | 88 | //left |
MarcusEA | 0:032a5658acdc | 89 | //leftDuty = 1500 - signedXAxis/2 + signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 90 | //leftDuty = 1500 -signedXAxis/2 -signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 91 | leftDuty = 1500 +signedXAxis/2 +signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 92 | |
MarcusEA | 0:032a5658acdc | 93 | |
MarcusEA | 0:032a5658acdc | 94 | if(leftDuty>2000) { |
MarcusEA | 0:032a5658acdc | 95 | leftDuty = 2000; |
MarcusEA | 0:032a5658acdc | 96 | } |
MarcusEA | 0:032a5658acdc | 97 | else if(leftDuty<1000) { |
MarcusEA | 0:032a5658acdc | 98 | leftDuty = 1000; |
MarcusEA | 0:032a5658acdc | 99 | } |
MarcusEA | 0:032a5658acdc | 100 | |
MarcusEA | 0:032a5658acdc | 101 | //rightDuty = 1500 + signedXAxis/2 + signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 102 | //rightDuty = 1500 + signedXAxis/2 - signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 103 | rightDuty = 1500 - signedXAxis/2 + signedYAxis/2; |
MarcusEA | 0:032a5658acdc | 104 | if(rightDuty>2000) { |
MarcusEA | 0:032a5658acdc | 105 | rightDuty = 2000; |
MarcusEA | 0:032a5658acdc | 106 | } |
MarcusEA | 0:032a5658acdc | 107 | else if(rightDuty<1000) { |
MarcusEA | 0:032a5658acdc | 108 | rightDuty = 1000; |
MarcusEA | 0:032a5658acdc | 109 | } |
MarcusEA | 0:032a5658acdc | 110 | |
MarcusEA | 0:032a5658acdc | 111 | pc.printf("DeadMan:ON, LeftDuty:%d,RightDuty:%d\n\r", leftDuty, rightDuty); |
MarcusEA | 0:032a5658acdc | 112 | } |
MarcusEA | 0:032a5658acdc | 113 | } |
MarcusEA | 0:032a5658acdc | 114 | } |
MarcusEA | 0:032a5658acdc | 115 | } |
MarcusEA | 0:032a5658acdc | 116 | |
MarcusEA | 0:032a5658acdc | 117 | //Sends message over the CANbus |
MarcusEA | 0:032a5658acdc | 118 | //- used in operationMode1() |
MarcusEA | 0:032a5658acdc | 119 | void writeCAN() |
MarcusEA | 0:032a5658acdc | 120 | { |
MarcusEA | 0:032a5658acdc | 121 | CANMessage msgOut; |
MarcusEA | 0:032a5658acdc | 122 | msgOut.format = CANExtended; |
MarcusEA | 0:032a5658acdc | 123 | msgOut.id = 0x71; //messageID |
MarcusEA | 0:032a5658acdc | 124 | msgOut.len = 8; //length in bytes |
MarcusEA | 0:032a5658acdc | 125 | |
MarcusEA | 0:032a5658acdc | 126 | msgOut.data[0] = (leftDuty>>8)%0xFF; |
MarcusEA | 0:032a5658acdc | 127 | msgOut.data[1] = leftDuty&0xFF; |
MarcusEA | 0:032a5658acdc | 128 | msgOut.data[2] = (rightDuty>>8)&0xFF; |
MarcusEA | 0:032a5658acdc | 129 | msgOut.data[3] = rightDuty&0xFF; |
MarcusEA | 0:032a5658acdc | 130 | msgOut.data[4] = deadMan; |
MarcusEA | 0:032a5658acdc | 131 | msgOut.data[5] = 0; |
MarcusEA | 0:032a5658acdc | 132 | msgOut.data[6] = 0; |
MarcusEA | 0:032a5658acdc | 133 | msgOut.data[7] = 0; |
MarcusEA | 0:032a5658acdc | 134 | can.write(msgOut); |
MarcusEA | 0:032a5658acdc | 135 | } |
MarcusEA | 0:032a5658acdc | 136 | |
MarcusEA | 0:032a5658acdc | 137 | int main() { |
MarcusEA | 0:032a5658acdc | 138 | pc.baud(115200); |
MarcusEA | 0:032a5658acdc | 139 | pc.printf("\r\n Starting CANBus Joystick to ESC PWM \r\n"); |
MarcusEA | 0:032a5658acdc | 140 | |
MarcusEA | 0:032a5658acdc | 141 | //PWM Enable @ 1500 default, 20000 period |
MarcusEA | 0:032a5658acdc | 142 | leftESC.Enable(1500,20000); |
MarcusEA | 0:032a5658acdc | 143 | rightESC.Enable(1500,20000); |
MarcusEA | 0:032a5658acdc | 144 | |
MarcusEA | 0:032a5658acdc | 145 | //CANBus @250kb/s |
MarcusEA | 0:032a5658acdc | 146 | can.frequency(250000); |
MarcusEA | 0:032a5658acdc | 147 | // can.filter(0x70,0x70,CANExtended); //CANbus filtering to only accept 0x70 |
MarcusEA | 0:032a5658acdc | 148 | |
MarcusEA | 0:032a5658acdc | 149 | wait(1.0); |
MarcusEA | 0:032a5658acdc | 150 | |
MarcusEA | 0:032a5658acdc | 151 | ticker.attach(&writeCAN, 0.1); |
MarcusEA | 0:032a5658acdc | 152 | |
MarcusEA | 0:032a5658acdc | 153 | while(true) { |
MarcusEA | 0:032a5658acdc | 154 | |
MarcusEA | 0:032a5658acdc | 155 | readCAN(); //read Joystick CANBus and adjust duty |
MarcusEA | 0:032a5658acdc | 156 | |
MarcusEA | 0:032a5658acdc | 157 | leftESC.SetPosition(leftDuty); //set duties for left |
MarcusEA | 0:032a5658acdc | 158 | rightESC.SetPosition(rightDuty); //set duties for right |
MarcusEA | 0:032a5658acdc | 159 | |
MarcusEA | 0:032a5658acdc | 160 | wait(0.01); |
MarcusEA | 0:032a5658acdc | 161 | |
MarcusEA | 0:032a5658acdc | 162 | //pc.printf("R: %d, L: %d\r\n", rightDuty, leftDuty); |
MarcusEA | 0:032a5658acdc | 163 | //wait(1); |
MarcusEA | 0:032a5658acdc | 164 | } |
MarcusEA | 0:032a5658acdc | 165 | |
MarcusEA | 0:032a5658acdc | 166 | } |
MarcusEA | 0:032a5658acdc | 167 | |
MarcusEA | 0:032a5658acdc | 168 | |
MarcusEA | 0:032a5658acdc | 169 | |
MarcusEA | 0:032a5658acdc | 170 |